-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathmd5.ahk
136 lines (124 loc) · 4.62 KB
/
md5.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
; ===================================================================================
; AHK Version ...: AHK_L 1.1.14.03 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Description ...: Checksum: MD5
; Calc MD5-Hash from String / Hex / File / Address
; http://en.wikipedia.org/wiki/MD5
; Version .......: 2014.02.26-1986
; Author ........: Bentschi
; Modified ......: jNizM
; ===================================================================================
/*
; GLOBAL SETTINGS ===================================================================
#Warn
#NoEnv
#SingleInstance Force
SetBatchLines, -1
; SCRIPT ============================================================================
str := "The quick brown fox jumps over the lazy dog"
MsgBox, % "String:`n" (str) "`n`nMD5:`n" MD5(str)
hex := "54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67"
MsgBox, % "Hex:`n" (hex) "`n`nMD5:`n" HexMD5(hex)
file := "C:\Windows\notepad.exe"
MsgBox, % "File:`n" (file) "`n`nMD5:`n" FileMD5(file)
ExitApp
*/
; MD5 ===============================================================================
MD5(string, encoding = "UTF-8")
{
return CalcStringHash(string, 0x8003, encoding)
}
HexMD5(hexstring)
{
return CalcHexHash(hexstring, 0x8003)
}
FileMD5(filename)
{
return CalcFileHash(filename, 0x8003, 64 * 1024)
}
AddrMD5(addr, length)
{
return CalcAddrHash(addr, length, 0x8003)
}
; CalcAddrHash ======================================================================
CalcAddrHash(addr, length, algid, byref hash = 0, byref hashlength = 0)
{
static h := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"]
static b := h.minIndex()
hProv := hHash := o := ""
if (DllCall("advapi32\CryptAcquireContext", "Ptr*", hProv, "Ptr", 0, "Ptr", 0, "UInt", 24, "UInt", 0xF0000000))
{
if (DllCall("advapi32\CryptCreateHash", "Ptr", hProv, "UInt", algid, "UInt", 0, "UInt", 0, "Ptr*", hHash))
{
if (DllCall("advapi32\CryptHashData", "Ptr", hHash, "Ptr", addr, "UInt", length, "UInt", 0))
{
if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "UInt", 2, "Ptr", 0, "UInt*", hashlength, "UInt", 0))
{
VarSetCapacity(hash, hashlength, 0)
if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "UInt", 2, "Ptr", &hash, "UInt*", hashlength, "UInt", 0))
{
loop % hashlength
{
v := NumGet(hash, A_Index - 1, "UChar")
o .= h[(v >> 4) + b] h[(v & 0xf) + b]
}
}
}
}
DllCall("advapi32\CryptDestroyHash", "Ptr", hHash)
}
DllCall("advapi32\CryptreleaseContext", "Ptr", hProv, "UInt", 0)
}
return o
}
; CalcStringHash ====================================================================
CalcStringHash(string, algid, encoding = "UTF-8", byref hash = 0, byref hashlength = 0)
{
chrlength := (encoding = "CP1200" || encoding = "UTF-16") ? 2 : 1
length := (StrPut(string, encoding) - 1) * chrlength
VarSetCapacity(data, length, 0)
StrPut(string, &data, floor(length / chrlength), encoding)
return CalcAddrHash(&data, length, algid, hash, hashlength)
}
; CalcHexHash =======================================================================
CalcHexHash(hexstring, algid)
{
length := StrLen(hexstring) // 2
VarSetCapacity(data, length, 0)
loop % length
{
NumPut("0x" SubStr(hexstring, 2 * A_Index -1, 2), data, A_Index - 1, "Char")
}
return CalcAddrHash(&data, length, algid)
}
; CalcFileHash ======================================================================
CalcFileHash(filename, algid, continue = 0, byref hash = 0, byref hashlength = 0)
{
fpos := ""
if (!(f := FileOpen(filename, "r")))
{
return
}
f.pos := 0
if (!continue && f.length > 0x7fffffff)
{
return
}
if (!continue)
{
VarSetCapacity(data, f.length, 0)
f.rawRead(&data, f.length)
f.pos := oldpos
return CalcAddrHash(&data, f.length, algid, hash, hashlength)
}
hashlength := 0
while (f.pos < f.length)
{
readlength := (f.length - fpos > continue) ? continue : f.length - f.pos
VarSetCapacity(data, hashlength + readlength, 0)
DllCall("RtlMoveMemory", "Ptr", &data, "Ptr", &hash, "Ptr", hashlength)
f.rawRead(&data + hashlength, readlength)
h := CalcAddrHash(&data, hashlength + readlength, algid, hash, hashlength)
}
return h
}