-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashing.pas
316 lines (271 loc) · 8.68 KB
/
hashing.pas
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
unit hashing;
// fibodevy 2025
// https://github.com/fibodevy
{$mode ObjFPC}{$H+}
{$modeswitch anonymousfunctions}
{$modeswitch functionreferences}
interface
uses SysUtils;
// helper functions
function tohex(p: pointer; l: dword): string;
function tohex(s: string): string;
function tobin(p: pointer; l: dword): string;
function tobin(b: tbytes): string;
// SHA hashes
function sha1(const s: rawbytestring): rawbytestring;
function sha256(const s: rawbytestring): rawbytestring;
// HMAC
type
thashfunc = reference to function(data: string): string;
function hash_hmac(hash: thashfunc; blocksize: dword; data: pbyte; datalen: dword; key: pbyte; keylen: dword; raw: boolean=false): string;
function hmac_sha1(data: pbyte; datalen: dword; key: pbyte; keylen: dword; raw: boolean=false): string;
function hmac_sha256(data: pbyte; datalen: dword; key: pbyte; keylen: dword; raw: boolean=false): string;
implementation
// helper functions
function tohex(p: pointer; l: dword): string;
var
i: integer;
begin
result := '';
for i := 0 to l-1 do result += inttohex(pbyte(p+i)^, 2);
result := lowercase(result);
end;
function tohex(s: string): string;
begin
result := tohex(@s[1], length(s));
end;
function tobin(p: pointer; l: dword): string;
begin
setlength(result, l);
move(p^, result[1], l);
end;
function tobin(b: tbytes): string;
begin
result := tobin(@b[0], length(b));
end;
// SHA1
type
TSHA1Contet = record
state: array[0..4] of dword;
count: int64;
buffer: array[0..63] of byte;
end;
procedure sha1init(var ctx: TSHA1Contet);
begin
ctx.state[0] := $67452301;
ctx.state[1] := $EFCDAB89;
ctx.state[2] := $98BADCFE;
ctx.state[3] := $10325476;
ctx.state[4] := $C3D2E1F0;
ctx.count := 0;
end;
function rol(v: longword; n: byte): longword;
begin
Result := (v shl n) or (v shr (32 - n));
end;
procedure sha1transform(var state: array of dword; const block: array of byte);
var
w: array[0..79] of dword;
a, b, c, d, e, f, k, temp: dword;
i: integer;
begin
for i := 0 to 15 do w[i] := (block[i * 4] shl 24) or (block[i * 4 + 1] shl 16) or (block[i * 4 + 2] shl 8) or block[i * 4 + 3];
for i := 16 to 79 do w[i] := rol(w[i - 3] xor w[i - 8] xor w[i - 14] xor w[i - 16], 1);
a := state[0]; b := state[1]; c := state[2]; d := state[3]; e := state[4];
for i := 0 to 79 do begin
if i < 20 then begin
f := (b and c) or ((not b) and d);
k := $5A827999;
end else if i < 40 then begin
f := b xor c xor d;
k := $6ED9EBA1;
end else if i < 60 then begin
f := (b and c) or (b and d) or (c and d);
k := $8F1BBCDC;
end else begin
f := b xor c xor d; k := $CA62C1D6;
end;
temp := rol(a, 5) + f + e + k + w[i];
e := d;
d := c;
c := rol(b, 30);
b := a;
a := temp;
end;
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
end;
procedure sha1update(var ctx: TSHA1Contet; const data: pbyte; len: integer);
var
i, j: integer;
begin
j := ctx.count shr 3 mod 64;
ctx.count += len * 8;
i := 0;
while len > 0 do begin
ctx.buffer[j] := data[i];
inc(j);
inc(i);
dec(len);
if j = 64 then begin
sha1transform(ctx.state, ctx.buffer);
j := 0;
end;
end;
end;
procedure sha1final(var ctx: TSHA1Contet; out digest: array of byte);
var
i: integer;
finalcount: array[0..7] of byte;
pad: array[0..63] of byte = (128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
begin
for i := 0 to 7 do finalcount[i] := (ctx.count shr ((7 - i) * 8)) and $FF;
sha1update(ctx, @pad[0], 1 + ((119 - (ctx.count shr 3) mod 64) mod 64));
sha1update(ctx, @finalcount[0], 8);
for i := 0 to 4 do begin
digest[i*4+0] := (ctx.state[i] shr 24) and $FF;
digest[i*4+1] := (ctx.state[i] shr 16) and $FF;
digest[i*4+2] := (ctx.state[i] shr 8) and $FF;
digest[i*4+3] := ctx.state[i] and $FF;
end;
end;
function sha1(const s: rawbytestring): rawbytestring;
var
ctx: TSHA1Contet;
hash: array[0..19] of byte;
begin
sha1init(ctx);
sha1update(ctx, @s[1], Length(s));
sha1final(ctx, hash);
setlength(result, 20);
move(hash[0], result[1], 20);
end;
// SHA256
type
TSHA256State = array[0..7] of dword;
const
K: array of dword = (
$428a2f98, $71374491, $b5c0fbcf, $e9b5dba5, $3956c25b, $59f111f1, $923f82a4, $ab1c5ed5,
$d807aa98, $12835b01, $243185be, $550c7dc3, $72be5d74, $80deb1fe, $9bdc06a7, $c19bf174,
$e49b69c1, $efbe4786, $0fc19dc6, $240ca1cc, $2de92c6f, $4a7484aa, $5cb0a9dc, $76f988da,
$983e5152, $a831c66d, $b00327c8, $bf597fc7, $c6e00bf3, $d5a79147, $06ca6351, $14292967,
$27b70a85, $2e1b2138, $4d2c6dfc, $53380d13, $650a7354, $766a0abb, $81c2c92e, $92722c85,
$a2bfe8a1, $a81a664b, $c24b8b70, $c76c51a3, $d192e819, $d6990624, $f40e3585, $106aa070,
$19a4c116, $1e376c08, $2748774c, $34b0bcb5, $391c0cb3, $4ed8aa4a, $5b9cca4f, $682e6ff3,
$748f82ee, $78a5636f, $84c87814, $8cc70208, $90befffa, $a4506ceb, $bef9a3f7, $c67178f2
);
function ror(x: dword; n: byte): dword;
begin
result := (x shr n) or (x shl (32 - n));
end;
procedure sha256transform(var state: TSHA256State; const block: array of byte);
var
w: array[0..63] of dword;
a, b, c, d, e, f, g, h, t1, t2: dword;
i: integer;
begin
for i := 0 to 15 do w[i] := (block[i*4] shl 24) or (block[i*4+1] shl 16) or (block[i*4+2] shl 8) or block[i*4+3];
for i := 16 to 63 do w[i] := (ror(w[i-15],7) xor ror(w[i-15],18) xor (w[i-15] shr 3))+w[i-16]+(ror(w[i-2],17) xor ror(w[i-2],19) xor (w[i-2] shr 10))+w[i-7];
a := state[0]; b := state[1]; c := state[2]; d := state[3];
e := state[4]; f := state[5]; g := state[6]; h := state[7];
for i := 0 to 63 do begin
t1 := h + (ror(e,6) xor ror(e,11) xor ror(e,25)) + ((e and f) xor ((not e) and g)) + K[i] + w[i];
t2 := (ror(a,2) xor ror(a,13) xor ror(a,22)) + ((a and b) xor (a and c) xor (b and c));
h := g; g := f; f := e; e := d + t1;
d := c; c := b; b := a; a := t1 + t2;
end;
state[0] += a; state[1] += b; state[2] += c; state[3] += d;
state[4] += e; state[5] += f; state[6] += g; state[7] += h;
end;
function sha256(const s: rawbytestring): rawbytestring;
var
state: TSHA256State = (
$6a09e667, $bb67ae85, $3c6ef372, $a54ff53a,
$510e527f, $9b05688c, $1f83d9ab, $5be0cd19
);
bitlen: qword;
buf: array[0..63] of byte;
i: integer;
begin
result := '';
bitlen := length(s) * 8;
FillChar(buf, sizeof(buf), 0);
i := 0;
// full blocks
while i + 63 < length(s) do begin
move(s[i+1], buf[0], 64);
sha256transform(state, buf);
inc(i, 64);
end;
// final block
FillChar(buf, SizeOf(buf), 0);
move(s[i+1], buf[0], length(s) - i);
buf[length(s)-i] := $80;
if (Length(s)-i) >= 56 then begin
sha256transform(state, buf);
FillChar(buf, sizeof(buf), 0);
end;
buf[63] := bitlen and $FF;
buf[62] := (bitlen shr 8) and $FF;
buf[61] := (bitlen shr 16) and $FF;
buf[60] := (bitlen shr 24) and $FF;
buf[59] := (bitlen shr 32) and $FF;
buf[58] := (bitlen shr 40) and $FF;
buf[57] := (bitlen shr 48) and $FF;
buf[56] := (bitlen shr 56) and $FF;
sha256transform(state, buf);
setlength(result, 32);
for i := 0 to 7 do begin
pbyte(@result[1+i*4+0])^ := (state[i] shr 24) and $FF;
pbyte(@result[1+i*4+1])^ := (state[i] shr 16) and $FF;
pbyte(@result[1+i*4+2])^ := (state[i] shr 8) and $FF;
pbyte(@result[1+i*4+3])^ := (state[i] and $ff);
end;
end;
// HMAC
function hash_hmac(hash: thashfunc; blocksize: dword; data: pbyte; datalen: dword; key: pbyte; keylen: dword; raw: boolean=false): string;
var
opad, ipad: tbytes;
i: integer;
s: string;
begin
// fix key
if keylen > blocksize then begin
setlength(s, keylen);
move(key^, s[1], keylen);
s := hash(s);
key := @s[1];
keylen := length(s);
end;
// make opad ipad
setlength(opad, blocksize);
fillchar(opad[0], blocksize, $5c);
setlength(ipad, blocksize);
fillchar(ipad[0], blocksize, $36);
for i := 0 to keylen-1 do begin
opad[i] := opad[i] xor ord(key[i]);
ipad[i] := ipad[i] xor ord(key[i]);
end;
// hash it
result := hash(tobin(opad)+hash(tobin(ipad)+tobin(data, datalen)));
if not raw then result := tohex(@result[1], length(result));
end;
function hmac_sha1(data: pbyte; datalen: dword; key: pbyte; keylen: dword; raw: boolean=false): string;
begin
result := hash_hmac(function(data: string): string
begin
result := sha1(data);
end, 64, data, datalen, key, keylen, raw);
end;
function hmac_sha256(data: pbyte; datalen: dword; key: pbyte; keylen: dword; raw: boolean=false): string;
begin
result := hash_hmac(function(data: string): string
begin
result := sha256(data);
end, 64, data, datalen, key, keylen, raw);
end;
end.