Skip to content

Commit

Permalink
Create HashFile.ahk
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierry Dalon committed Oct 20, 2023
1 parent c9629ad commit 97e6049
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions Lib/HashFile.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
HashFile(filePath,hashType=2) ; By Deo, http://www.autohotkey.com/forum/viewtopic.php?t=71133
; Example: hash := HashFile(filePath, "MD5")
{
PROV_RSA_AES := 24
CRYPT_VERIFYCONTEXT := 0xF0000000
BUFF_SIZE := 1024 * 1024 ; 1 MB
HP_HASHVAL := 0x0002
HP_HASHSIZE := 0x0004

HASH_ALG := (hashType = 1 OR hashType = "MD2") ? (CALG_MD2 := 32769) : HASH_ALG
HASH_ALG := (hashType = 2 OR hashType = "MD5") ? (CALG_MD5 := 32771) : HASH_ALG
HASH_ALG := (hashType = 3 OR hashType = "SHA") ? (CALG_SHA := 32772) : HASH_ALG
HASH_ALG := (hashType = 4 OR hashType = "SHA256") ? (CALG_SHA_256 := 32780) : HASH_ALG ;Vista+ only
HASH_ALG := (hashType = 5 OR hashType = "SHA384") ? (CALG_SHA_384 := 32781) : HASH_ALG ;Vista+ only
HASH_ALG := (hashType = 6 OR hashType = "SHA512") ? (CALG_SHA_512 := 32782) : HASH_ALG ;Vista+ only

f := FileOpen(filePath,"r","CP0")
if !IsObject(f)
return 0
if !hModule := DllCall( "GetModuleHandleW", "str", "Advapi32.dll", "Ptr" )
hModule := DllCall( "LoadLibraryW", "str", "Advapi32.dll", "Ptr" )
if !dllCall("Advapi32\CryptAcquireContextW"
,"Ptr*",hCryptProv
,"Uint",0
,"Uint",0
,"Uint",PROV_RSA_AES
,"UInt",CRYPT_VERIFYCONTEXT )
Gosub,HashTypeFreeHandles

if !dllCall("Advapi32\CryptCreateHash"
,"Ptr",hCryptProv
,"Uint",HASH_ALG
,"Uint",0
,"Uint",0
,"Ptr*",hHash )
Gosub, HashTypeFreeHandles

VarSetCapacity(read_buf,BUFF_SIZE,0)

hCryptHashData := DllCall("GetProcAddress", "Ptr", hModule, "AStr", "CryptHashData", "Ptr")
While (cbCount := f.RawRead(read_buf, BUFF_SIZE))
{
if (cbCount = 0)
break

if !dllCall(hCryptHashData
,"Ptr",hHash
,"Ptr",&read_buf
,"Uint",cbCount
,"Uint",0 )
Gosub, HashTypeFreeHandles
}

if !dllCall("Advapi32\CryptGetHashParam"
,"Ptr",hHash
,"Uint",HP_HASHSIZE
,"Uint*",HashLen
,"Uint*",HashLenSize := 4
,"UInt",0 )
Gosub, HashTypeFreeHandles

VarSetCapacity(pbHash,HashLen,0)
if !dllCall("Advapi32\CryptGetHashParam"
,"Ptr",hHash
,"Uint",HP_HASHVAL
,"Ptr",&pbHash
,"Uint*",HashLen
,"UInt",0 )
Gosub, HashTypeFreeHandles

SetFormat,integer,Hex
loop,%HashLen%
{
num := numget(pbHash,A_index-1,"UChar")
hashval .= substr((num >> 4),0) . substr((num & 0xf),0)
}
SetFormat,integer,D

HashTypeFreeHandles:
f.Close()
DllCall("FreeLibrary", "Ptr", hModule)
dllCall("Advapi32\CryptDestroyHash","Ptr",hHash)
dllCall("Advapi32\CryptReleaseContext","Ptr",hCryptProv,"UInt",0)
return hashval
}

0 comments on commit 97e6049

Please sign in to comment.