Computes and returns CRC32 hash for a File passed as parameter.
Please refer FileMD5() for explanation on parameter 2
MsgBox, % FileCRC32( A_AhkPath )[color=#408000] ; Usage Example[/color]
[color=#FF0000]FileCRC32([/color] sFile="",cSz=4 [color=#FF0000])[/color] { ; by SKAN www.autohotkey.com/community/viewtopic.php?t=64211 cSz := (cSz<0||cSz>8) ? 2**22 : 2**(18+cSz), VarSetCapacity( Buffer,cSz,0 ) ; 10-Oct-2009 hFil := DllCall( "CreateFile", Str,sFile,UInt,0x80000000, Int,3,Int,0,Int,3,Int,0,Int,0 ) IfLess,hFil,1, Return,hFil hMod := DllCall( "LoadLibrary", Str,"ntdll.dll" ), CRC32 := 0 DllCall( "GetFileSizeEx", UInt,hFil, UInt,&Buffer ), fSz := NumGet( Buffer,0,"Int64" ) Loop % ( fSz//cSz + !!Mod( fSz,cSz ) ) DllCall( "ReadFile", UInt,hFil, UInt,&Buffer, UInt,cSz, UIntP,Bytes, UInt,0 ) , CRC32 := DllCall( "NTDLL\RtlComputeCrc32", UInt,CRC32, UInt,&Buffer, UInt,Bytes, UInt ) DllCall( "CloseHandle", UInt,hFil ) SetFormat, Integer, % SubStr( ( A_FI := A_FormatInteger ) "H", 0 ) CRC32 := SubStr( CRC32 + 0x1000000000, -7 ), DllCall( "CharUpper", Str,CRC32 ) SetFormat, Integer, %A_FI% Return CRC32, DllCall( "FreeLibrary", UInt,hMod ) }
FileSHA1()
Computes and returns SHA1 hash for a File passed as parameter.
Please refer FileMD5() for explanation on parameter 2
MsgBox, % FileSHA1( A_AhkPath )[color=#408000] ; Usage Example[/color]
[color=#FF0000]FileSHA1([/color] sFile="", cSz=4 [color=#FF0000])[/color] { ; by SKAN www.autohotkey.com/community/viewtopic.php?t=64211 cSz := (cSz<0||cSz>8) ? 2**22 : 2**(18+cSz), VarSetCapacity( Buffer,cSz,0 ) ; 09-Oct-2012 hFil := DllCall( "CreateFile", Str,sFile,UInt,0x80000000, Int,3,Int,0,Int,3,Int,0,Int,0 ) IfLess,hFil,1, Return,hFil hMod := DllCall( "LoadLibrary", Str,"advapi32.dll" ) DllCall( "GetFileSizeEx", UInt,hFil, UInt,&Buffer ), fSz := NumGet( Buffer,0,"Int64" ) VarSetCapacity( SHA_CTX,136,0 ), DllCall( "advapi32\A_SHAInit", UInt,&SHA_CTX ) Loop % ( fSz//cSz + !!Mod( fSz,cSz ) ) DllCall( "ReadFile", UInt,hFil, UInt,&Buffer, UInt,cSz, UIntP,bytesRead, UInt,0 ) , DllCall( "advapi32\A_SHAUpdate", UInt,&SHA_CTX, UInt,&Buffer, UInt,bytesRead ) DllCall( "advapi32\A_SHAFinal", UInt,&SHA_CTX, UInt,&SHA_CTX + 116 ) DllCall( "CloseHandle", UInt,hFil ) Loop % StrLen( Hex:="123456789ABCDEF0" ) + 4 N := NumGet( SHA_CTX,115+A_Index,"Char"), SHA1 .= SubStr(Hex,N>>4,1) SubStr(Hex,N&15,1) Return SHA1, DllCall( "FreeLibrary", UInt,hMod ) }
FileMD5()
Computes and returns MD5 hash [ RFC1321 Specification ] for a File passed as parameter, with speeds comparable to Hashes.DLL.
You may refer MSDN for Message Digest API ( Edit 09-Oct-2012: MSDN has removed documention for MD5 )
Parameters:
sFile : The fullpath of filename to hash
cSz : *Chunk size factor. Values accepted are 0 thru 8. Default value is 4
* To hash large files, the function has to read the file in manageable chunks into a buffer variable.
The size of this buffer is derived from cSz parameter as follows:
0 = 256 KB, 1 = 512 KB, 2 = 1.00 MB, 3 = 2.00 MB, 4 = 4.00 MB, 5 = 8.00 MB, 6 = 16.0 MB, 7 = 32.0 MB, 8 = 64.0 MB
MsgBox, % FileMD5( A_AhkPath )[color=#408000] ; Usage Example[/color]
[color=#FF0000]FileMD5([/color] sFile="", cSz=4 [color=#FF0000])[/color] { ; by SKAN www.autohotkey.com/community/viewtopic.php?t=64211 cSz := (cSz<0||cSz>8) ? 2**22 : 2**(18+cSz), VarSetCapacity( Buffer,cSz,0 ) ; 18-Jun-2009 hFil := DllCall( "CreateFile", Str,sFile,UInt,0x80000000, Int,3,Int,0,Int,3,Int,0,Int,0 ) IfLess,hFil,1, Return,hFil hMod := DllCall( "LoadLibrary", Str,"advapi32.dll" ) DllCall( "GetFileSizeEx", UInt,hFil, UInt,&Buffer ), fSz := NumGet( Buffer,0,"Int64" ) VarSetCapacity( MD5_CTX,104,0 ), DllCall( "advapi32\MD5Init", UInt,&MD5_CTX ) Loop % ( fSz//cSz + !!Mod( fSz,cSz ) ) DllCall( "ReadFile", UInt,hFil, UInt,&Buffer, UInt,cSz, UIntP,bytesRead, UInt,0 ) , DllCall( "advapi32\MD5Update", UInt,&MD5_CTX, UInt,&Buffer, UInt,bytesRead ) DllCall( "advapi32\MD5Final", UInt,&MD5_CTX ) DllCall( "CloseHandle", UInt,hFil ) Loop % StrLen( Hex:="123456789ABCDEF0" ) N := NumGet( MD5_CTX,87+A_Index,"Char"), MD5 .= SubStr(Hex,N>>4,1) . SubStr(Hex,N&15,1) Return MD5, DllCall( "FreeLibrary", UInt,hMod ) }
MD5()
Computes and returns MD5 hash [ RFC1321 Specification ] for memory variable contents.
[color=#FF0000]MD5([/color] ByRef V, L=0 [color=#FF0000])[/color] { ; www.autohotkey.com/forum/viewtopic.php?p=275910#275910 VarSetCapacity( MD5_CTX,104,0 ), DllCall( "[color=#D62A00]advapi32\MD5Init[/color]", Str,MD5_CTX ) DllCall( "[color=#D62A00]advapi32\MD5Update[/color]", Str,MD5_CTX, Str,V, UInt,L ? L : StrLen(V) ) DllCall( "[color=#D62A00]advapi32\MD5Final[/color]", Str,MD5_CTX ) Loop % StrLen( Hex:="123456789ABCDEF0" ) N := NumGet( MD5_CTX,87+A_Index,"Char"), MD5 .= SubStr(Hex,N>>4,1) . SubStr(Hex,N&15,1) Return MD5 } ; Usage Example [color=black]V := "The quick brown fox jumps over the lazy dog" L := StrLen(V) MsgBox, % MD5( V,L )[/color]