-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathHashFile.ahk
106 lines (91 loc) · 2.64 KB
/
HashFile.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
; HASH File by Deo
; Current code requires AHK_L, but can be converted for using with common ahk build.
; Link: https://autohotkey.com/board/topic/66139-ahk-l-calculating-md5sha-checksum-from-file/
/*
FilePath := "C:\Windows\notepad.exe"
msgbox % "MD5:`n" HashFile(filePath,2)
msgbox % "SHA:`n" HashFile(filePath,3)
msgbox % "SHA512:`n" HashFile(filePath,6)
*/
/*
HASH types:
1 - MD2
2 - MD5
3 - SHA
4 - SHA256 - not supported on XP,2000
5 - SHA384 - not supported on XP,2000
6 - SHA512 - not supported on XP,2000
*/
HashFile(filePath,hashType=2) {
PROV_RSA_AES := 24
CRYPT_VERIFYCONTEXT := 0xF0000000
BUFF_SIZE := 1024 * 1024 ; 1 MB
HP_HASHVAL := 0x0002
HP_HASHSIZE := 0x0004
HASH_ALG := hashType = 1 ? (CALG_MD2 := 32769) : HASH_ALG
HASH_ALG := hashType = 2 ? (CALG_MD5 := 32771) : HASH_ALG
HASH_ALG := hashType = 3 ? (CALG_SHA := 32772) : HASH_ALG
HASH_ALG := hashType = 4 ? (CALG_SHA_256 := 32780) : HASH_ALG ;Vista+ only
HASH_ALG := hashType = 5 ? (CALG_SHA_384 := 32781) : HASH_ALG ;Vista+ only
HASH_ALG := hashType = 6 ? (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 )
Goto,FreeHandles
if !dllCall("Advapi32\CryptCreateHash"
,"Ptr",hCryptProv
,"Uint",HASH_ALG
,"Uint",0
,"Uint",0
,"Ptr*",hHash )
Goto,FreeHandles
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 )
Goto,FreeHandles
}
if !dllCall("Advapi32\CryptGetHashParam"
,"Ptr",hHash
,"Uint",HP_HASHSIZE
,"Uint*",HashLen
,"Uint*",HashLenSize := 4
,"UInt",0 )
Goto,FreeHandles
VarSetCapacity(pbHash,HashLen,0)
if !dllCall("Advapi32\CryptGetHashParam"
,"Ptr",hHash
,"Uint",HP_HASHVAL
,"Ptr",&pbHash
,"Uint*",HashLen
,"UInt",0 )
Goto,FreeHandles
SetFormat,integer,Hex
loop,%HashLen%
{
num := numget(pbHash,A_index-1,"UChar")
hashval .= substr((num >> 4),0) . substr((num & 0xf),0)
}
SetFormat,integer,D
FreeHandles:
f.Close()
DllCall("FreeLibrary", "Ptr", hModule)
dllCall("Advapi32\CryptDestroyHash","Ptr",hHash)
dllCall("Advapi32\CryptReleaseContext","Ptr",hCryptProv,"UInt",0)
return hashval
}