-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Hash.Common.pas
678 lines (548 loc) · 19.7 KB
/
JPL.Hash.Common.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
unit JPL.Hash.Common;
{$I .\..\jp.inc}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
uses
{$IFDEF MSWINDOWS}Windows,{$ENDIF}
SysUtils, Classes, JPL.Strings, JPL.Conversion
;
const
ERR_OPEN_FILE = 'Can not open file "%s"';
ERR_ALLOC_BUFFER = 'Can not allocate hash buffer';
ERR_INVALID_HASH_TYPE = 'Invalid hash type';
ERR_ABORTED = 'Aborted';
{$region ' DIGEST_LEN '}
DIGEST_LEN_BITS_128 = 16; // 128 bits / 16 bytes
DIGEST_LEN_BITS_160 = 20; // 160 bits / 20 bytes
DIGEST_LEN_BITS_224 = 28; // 224 bits / 28 bytes
DIGEST_LEN_BITS_256 = 32; // 256 bits / 32 bytes
DIGEST_LEN_BITS_384 = 48; // 384 bits / 48 bytes
DIGEST_LEN_BITS_512 = 64; // 512 bits / 64 bytes
DIGEST_LEN_CRC16 = 2; // 16 bits / 2 bytes
DIGEST_LEN_CRC24 = 3; // 24 bits / 3 bytes
DIGEST_LEN_CRC32 = 4; // 32 bits / 4 bytes
DIGEST_LEN_CRC64 = 8; // 64 bits / 8 bytes
DIGEST_LEN_ADLER32 = 4; // 32 bits / 4 bytes
DIGEST_LEN_MD2 = DIGEST_LEN_BITS_128;
DIGEST_LEN_MD4 = DIGEST_LEN_BITS_128;
DIGEST_LEN_MD5 = DIGEST_LEN_BITS_128;
DIGEST_LEN_RIPEMD = DIGEST_LEN_BITS_128;
DIGEST_LEN_RIPEMD128 = DIGEST_LEN_BITS_128;
DIGEST_LEN_RIPEMD160 = DIGEST_LEN_BITS_160;
DIGEST_LEN_RIPEMD256 = DIGEST_LEN_BITS_256;
DIGEST_LEN_RIPEMD320 = 40; // 320 bits / 40 bytes
DIGEST_LEN_SHA0 = DIGEST_LEN_BITS_160;
DIGEST_LEN_SHA1 = DIGEST_LEN_BITS_160;
DIGEST_LEN_SHA2_224 = DIGEST_LEN_BITS_224;
DIGEST_LEN_SHA2_256 = DIGEST_LEN_BITS_256;
DIGEST_LEN_SHA2_384 = DIGEST_LEN_BITS_384;
DIGEST_LEN_SHA2_512 = DIGEST_LEN_BITS_512;
DIGEST_LEN_SHA2_512_224 = DIGEST_LEN_BITS_224;
DIGEST_LEN_SHA2_512_256 = DIGEST_LEN_BITS_256;
DIGEST_LEN_SHA3_224 = DIGEST_LEN_BITS_224;
DIGEST_LEN_SHA3_256 = DIGEST_LEN_BITS_256;
DIGEST_LEN_SHA3_384 = DIGEST_LEN_BITS_384;
DIGEST_LEN_SHA3_512 = DIGEST_LEN_BITS_512;
DIGEST_LEN_SHAKE128 = DIGEST_LEN_BITS_256;
DIGEST_LEN_SHAKE256 = DIGEST_LEN_BITS_512;
DIGEST_LEN_WHIRLPOOL = DIGEST_LEN_BITS_512;
DIGEST_LEN_SNEFRU128 = DIGEST_LEN_BITS_128;
DIGEST_LEN_SNEFRU256 = DIGEST_LEN_BITS_256;
DIGEST_LEN_EDONR256 = DIGEST_LEN_BITS_256;
DIGEST_LEN_EDONR512 = DIGEST_LEN_BITS_512;
DIGEST_LEN_GOST = DIGEST_LEN_BITS_256;
DIGEST_LEN_GOST_CRYPTOPRO = DIGEST_LEN_BITS_256;
DIGEST_LEN_HAS160 = DIGEST_LEN_BITS_160;
DIGEST_LEN_TIGER = 24; // 192 bits / 24 bytes
DIGEST_LEN_BTIH = DIGEST_LEN_BITS_160;
DIGEST_LEN_ED2K = DIGEST_LEN_BITS_128;
DIGEST_LEN_AICH = DIGEST_LEN_BITS_128;
DIGEST_LEN_HAVAL = DIGEST_LEN_BITS_256;
{$endregion DIGEST_LEN}
{$region ' HASH_LEN '}
HASH_LEN_CRC16 = DIGEST_LEN_CRC16 * 2;
HASH_LEN_CRC24 = DIGEST_LEN_CRC24 * 2;
HASH_LEN_CRC32 = DIGEST_LEN_CRC32 * 2;
HASH_LEN_CRC64 = DIGEST_LEN_CRC64 * 2;
HASH_LEN_ADLER32 = DIGEST_LEN_ADLER32 * 2;
HASH_LEN_MD2 = DIGEST_LEN_MD2 * 2;
HASH_LEN_MD4 = DIGEST_LEN_MD4 * 2;
HASH_LEN_MD5 = DIGEST_LEN_MD5 * 2;
HASH_LEN_RIPEMD = DIGEST_LEN_RIPEMD * 2;
HASH_LEN_RIPEMD128 = DIGEST_LEN_RIPEMD128 * 2;
HASH_LEN_RIPEMD160 = DIGEST_LEN_RIPEMD160 * 2;
HASH_LEN_RIPEMD256 = DIGEST_LEN_RIPEMD256 * 2;
HASH_LEN_RIPEMD320 = DIGEST_LEN_RIPEMD320 * 2;
HASH_LEN_SHA0 = DIGEST_LEN_SHA0 * 2;
HASH_LEN_SHA1 = DIGEST_LEN_SHA1 * 2;
HASH_LEN_SHA2_224 = DIGEST_LEN_SHA2_224 * 2;
HASH_LEN_SHA2_256 = DIGEST_LEN_SHA2_256 * 2;
HASH_LEN_SHA2_384 = DIGEST_LEN_SHA2_384 * 2;
HASH_LEN_SHA2_512 = DIGEST_LEN_SHA2_512 * 2;
HASH_LEN_SHA2_512_224 = DIGEST_LEN_SHA2_512_224 * 2;
HASH_LEN_SHA2_512_256 = DIGEST_LEN_SHA2_512_256 * 2;
HASH_LEN_SHA3_224 = DIGEST_LEN_SHA3_224 * 2;
HASH_LEN_SHA3_256 = DIGEST_LEN_SHA3_256 * 2;
HASH_LEN_SHA3_384 = DIGEST_LEN_SHA3_384 * 2;
HASH_LEN_SHA3_512 = DIGEST_LEN_SHA3_512 * 2;
HASH_LEN_SHAKE128 = DIGEST_LEN_SHAKE128 * 2;
HASH_LEN_SHAKE256 = DIGEST_LEN_SHAKE256 * 2;
HASH_LEN_WHIRLPOOL = DIGEST_LEN_WHIRLPOOL * 2;
HASH_LEN_SNEFRU128 = DIGEST_LEN_SNEFRU128 * 2;
HASH_LEN_SNEFRU256 = DIGEST_LEN_SNEFRU256 * 2;
HASH_LEN_EDONR256 = DIGEST_LEN_EDONR256 * 2;
HASH_LEN_EDONR512 = DIGEST_LEN_EDONR512 * 2;
HASH_LEN_GOST = DIGEST_LEN_GOST * 2;
HASH_LEN_GOST_CRYPTOPRO = DIGEST_LEN_GOST_CRYPTOPRO * 2;
HASH_LEN_HAS160 = DIGEST_LEN_HAS160 * 2;
HASH_LEN_TIGER = DIGEST_LEN_TIGER * 2;
HASH_LEN_BTIH = DIGEST_LEN_BTIH * 2;
HASH_LEN_ED2K = DIGEST_LEN_ED2K * 2;
HASH_LEN_AICH = DIGEST_LEN_AICH * 2;
HASH_LEN_HAVAL = DIGEST_LEN_HAVAL * 2;
{$endregion HASH_LEN}
MIN_HASH_LEN = HASH_LEN_CRC16;
HASH_DEF_BUF_SIZE = High(Word);
type
THashEnumProc = function(PercentComplete: integer; BufNo: integer): Boolean;
TJPHashType = (
htNone,
htCrc16, htCrc24, htCrc32, htCrc64, htAdler32,
htMd2, htMd4, htMd5, htRipeMD, htRipeMD128, htRipeMD160, htRipeMD256, htRipeMD320,
htSha0, htSha1,
htSha2_224, htSha2_256, htSha2_384, htSha2_512, htSha2_512_224, htSha2_512_256,
htSha3_224, htSha3_256, htSha3_384, htSha3_512,
htShake128, htShake256,
htWhirlpool,
htSnefru128, htSnefru256,
htEdonr256, htEdonr512,
htGost, htGostCryptopro,
htHas160,
htTiger,
//htTTH,
//htBTIH,
htED2K,
//htAICH,
htHaval
);
THashTypeDynArray = array of TJPHashType;
THashResultRec = record
ValidHash: Boolean;
HashType: TJPHashType;
StrValueUpper: string;
StrValueLower: string;
IntValue: integer;
Int64Value: Int64;
StreamSize: Int64;
ElapsedTimeMs: DWORD;
SpeedMBperSec: Single;
procedure Clear;
end;
THashCheckRec = record
ValidChars: Boolean;
ValidLen: Boolean;
ExpectedLen: integer;
end;
var
//HashBufferSize: integer = (1024 * 800) - 1; // 800 KB
HashBufferSize: integer = (1024 * 1024); //
function FormatHash(HashStr: string; bUpperCase: Boolean; ByteSeparator: string = ''): string;
function IsValidHashStr(const Hash: string): Boolean;
function IsValidHashLen(const Hash: string; HashType: TJPHashType): Boolean;
function GetHashStrLength(HashType: TJPHashType; ErrorResult: integer = -1): integer;
function GetHashDigestLength(const HashType: TJPHashType; ErrorResult: integer = -1): integer;
function GetHashByteLength(const HashType: TJPHashType; ErrorResult: integer = -1): integer;
function GetHashBitLength(const HashType: TJPHashType; ErrorResult: integer = -1): integer;
// jeśli wszystko OK, CheckHash zwraca True, w przeciwnym razie zwraca False i dodatkowe informacje w HashCheckRec
function CheckHash(Hash: string; HashType: TJPHashType; var HashCheckRec: THashCheckRec): Boolean;
function HashTypeToStr(const HashType: TJPHashType; StrUnknownHash: string = ''): string;
function StrToHashType(s: string; Default: TJPHashType = htNone): TJPHashType;
procedure ClearHashResultRec(var hrr: THashResultRec);
function IsChecksum(const HashType: TJPHashType): Boolean;
function IsMDHash(const HashType: TJPHashType): Boolean;
function IsSha2Hash(const HashType: TJPHashType): Boolean;
function IsSha3Hash(const HashType: TJPHashType): Boolean;
{$IFDEF DCC} function JPFormat(const Msg: string; const Args: array of const): string; {$ENDIF}
function HashReverseBytes(A: LongInt): LongInt; {-rotate byte of LongInt}
function GetSpeedValue_MB_per_sec(const FileSize: Int64; const ElapsedTimeMs: DWORD; ErrorValue: Double = 0): Double;
function GetPossibleHashes(HashStr: string; bAddZeroIfOddLength: Boolean = True): THashTypeDynArray;
implementation
function GetPossibleHashes(HashStr: string; bAddZeroIfOddLength: Boolean = True): THashTypeDynArray;
var
Len: integer;
procedure Add(const ht: TJPHashType);
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := ht;
end;
begin
SetLength(Result, 0);
HashStr := RemoveAll(HashStr, ' ');
HashStr := RemoveAll(HashStr, '-');
Len := Length(HashStr);
if Odd(Len) then
if bAddZeroIfOddLength then HashStr := '0' + HashStr
else Exit;
case Len of
4: Add(htCrc16);
6: Add(htCrc24);
8:
begin
Add(htCrc32);
Add(htAdler32);
end;
16: Add(htCrc64);
32:
begin
Add(htMd2);
Add(htMd4);
Add(htMd5);
Add(htRipeMD);
Add(htRipeMD128);
Add(htSnefru128);
Add(htED2K);
//Add(htAICH);
end;
40:
begin
Add(htRipeMD160);
Add(htSha0);
Add(htSha1);
Add(htHas160);
//Add(htBTIH);
end;
48: Add(htTiger);
56:
begin
Add(htSha2_224);
Add(htSha2_512_224);
Add(htSha3_224);
end;
64:
begin
Add(htRipeMD256);
Add(htSha2_256);
Add(htSha2_512_256);
Add(htSha3_256);
Add(htShake128);
Add(htSnefru256);
Add(htEdonr256);
Add(htGost);
Add(htGostCryptopro);
Add(htHaval);
end;
80: Add(htRipeMD320);
96:
begin
Add(htSha2_384);
Add(htSha3_384);
end;
128:
begin
Add(htSha2_512);
Add(htSha3_512);
Add(htShake256);
Add(htWhirlpool);
end;
end;
end;
function HashReverseBytes(A: LongInt): LongInt; {-rotate byte of LongInt}
begin
Result := (A shr 24) or ((A shr 8) and $FF00) or ((A shl 8) and $FF0000) or (A shl 24);
end;
{$region ' JPFormat '}
{$IFDEF DCC}
function JPFormat(const Msg: string; const Args: array of const): string;
var
i: integer;
sr: string;
begin
sr := Msg;
for i := Low(Args) to High(Args) do
case TVarRec(Args[i]).VType of
vtInteger: sr := StringReplace(sr, '%d', IntToStr(Args[i].VInteger), []);
vtInt64: sr := StringReplace(sr, '%d', IntToStr(Args[i].VInt64^), []);
vtExtended: sr := StringReplace(sr, '%f', FloatToStr(Args[i].VExtended^), []);
vtString: sr := StringReplace(sr, '%s', string(Args[i].VString^), []);
vtAnsiString: sr := StringReplace(sr, '%s', string(AnsiString(Args[i].VAnsiString)), []);
vtWideString: sr := StringReplace(sr, '%s', WideString(Args[i].VWideString), []);
vtUnicodeString: sr := StringReplace(sr, '%s', UnicodeString(Args[i].VUnicodeString), []);
vtPointer: sr := StringReplace(sr, '%p', IntToHex(integer(@Args[i].VPointer), 8), []);
vtPChar: sr := StringReplace(sr, '%s', string(Args[i].VPChar), []);
vtPWideChar: sr := StringReplace(sr, '%s', string(Args[i].VPWideChar), []);
vtChar: sr := StringReplace(sr, '%c', string(Args[i].VChar), []);
vtWideChar: sr := StringReplace(sr, '%c', string(Args[i].VWideChar), []);
vtCurrency: sr := StringReplace(sr, '%m', FloatToStr(Args[i].VCurrency^), []);
vtBoolean: sr := StringReplace(sr, '%b', BoolToStr(Args[i].VBoolean, 'Yes', 'No'), []);
vtObject: sr := StringReplace(sr, '%o', TObject(Args[i].VObject).ToString, []);
//vtVariant: sr := StringReplace(sr, '%v', Args[i].VVariant^, []); // too big exe!
vtVariant: sr := StringReplace(sr, '%v', 'VARIANT TYPES NOT SUPPORTED', []);
else
//Writeln('UNKNOWN data type');
end;
Result := sr;
end;
{$ENDIF}
{$endregion JPFormat}
function GetSpeedValue_MB_per_sec(const FileSize: Int64; const ElapsedTimeMs: DWORD; ErrorValue: Double = 0): Double;
var
xMB, xSecs: Double;
begin
xMB := FileSize / 1024 / 1024;
xSecs := ElapsedTimeMs / 1000;
if xSecs > 0 then Result := xMB / xSecs
else Result := ErrorValue
end;
procedure ClearHashResultRec(var hrr: THashResultRec);
begin
hrr.Clear;
end;
function IsChecksum(const HashType: TJPHashType): Boolean;
begin
case HashType of
htCrc16, htCrc24, htCrc32, htCrc64, htAdler32: Result := True;
else
Result := False;
end;
end;
function IsMDHash(const HashType: TJPHashType): Boolean;
begin
case HashType of
htMd2, htMd4, htMd5, htRipeMD, htRipeMD128, htRipeMD160, htRipeMD256, htRipeMD320: Result := True;
else
Result := False;
end;
end;
function IsSha2Hash(const HashType: TJPHashType): Boolean;
begin
case HashType of
htSha2_224, htSha2_256, htSha2_384, htSha2_512, htSha2_512_224, htSha2_512_256: Result := True;
else
Result := False;
end;
end;
function IsSha3Hash(const HashType: TJPHashType): Boolean;
begin
case HashType of
htSha3_224, htSha3_256, htSha3_384, htSha3_512: Result := True;
else
Result := False;
end;
end;
function StrToHashType(s: string; Default: TJPHashType = htNone): TJPHashType;
begin
s := RemoveAll(s, ' ');
s := RemoveAll(s, '-');
s := RemoveAll(s, '_');
s := ReplaceAll(s, '\', '');
s := ReplaceAll(s, '/', '');
s := LowerCase(s);
if (s = '') or (s = 'none') then Result := htNone
else if s = 'crc16' then Result := htCrc16
else if s = 'crc24' then Result := htCrc24
else if s = 'crc32' then Result := htCrc32
else if s = 'crc64' then Result := htCrc64
else if s = 'adler32' then Result := htAdler32
else if s = 'md2' then Result := htMd2
else if s = 'md4' then Result := htMd4
else if s = 'md5' then Result := htMd5
else if s = 'ripemd' then Result := htRipeMD
else if (s = 'ripemd128') or (s = 'rmd128') then Result := htRipeMD128
else if (s = 'ripemd160') or (s = 'rmd160') then Result := htRipeMD160
else if (s = 'ripemd256') or (s = 'rmd256') then Result := htRipeMD256
else if (s = 'ripemd320') or (s = 'rmd320') then Result := htRipeMD320
else if s = 'sha0' then Result := htSha0
else if s = 'sha1' then Result := htSha1
else if (s = 'sha2224') or (s = 'sha224') then Result := htSha2_224
else if (s = 'sha2256') or (s = 'sha256') then Result := htSha2_256
else if (s = 'sha2384') or (s = 'sha384') then Result := htSha2_384
else if (s = 'sha2512') or (s = 'sha512') then Result := htSha2_512
else if (s = 'sha2512224') then Result := htSha2_512_224
else if (s = 'sha2512256') then Result := htSha2_512_256
else if s = 'sha3224' then Result := htSha3_224
else if s = 'sha3256' then Result := htSha3_256
else if s = 'sha3384' then Result := htSha3_384
else if s = 'sha3512' then Result := htSha3_512
else if s = 'shake128' then Result := htShake128
else if s = 'shake256' then Result := htShake256
else if s = 'whirlpool' then Result := htWhirlpool
else if s = 'snefru128' then Result := htSnefru128
else if s = 'snefru256' then Result := htSnefru256
else if s = 'edonr256' then Result := htEdonr256
else if s = 'edonr512' then Result := htEdonr512
else if s = 'gost' then Result := htGost
else if s = 'gostcryptopro' then Result := htGostCryptopro
else if s = 'has160' then Result := htHas160
else if s = 'tiger' then Result := htTiger
else if s = 'ed2k' then Result := htED2K
else if s = 'haval' then Result := htHaval
else Result := Default;
end;
function HashTypeToStr(const HashType: TJPHashType; StrUnknownHash: string = ''): string;
begin
case HashType of
htCrc16: Result := 'CRC16';
htCrc24: Result := 'CRC24';
htCrc32: Result := 'CRC32';
htCrc64: Result := 'CRC64';
htAdler32: Result := 'Adler32';
htMd2: Result := 'MD2';
htMd4: Result := 'MD4';
htMd5: Result := 'MD5';
htRipeMD: Result := 'RipeMD';
htRipeMD128: Result := 'RipeMD-128';
htRipeMD160: Result := 'RipeMD-160';
htRipeMD256: Result := 'RipeMD-256';
htRipeMD320: Result := 'RipeMD-320';
htSha0: Result := 'SHA-0';
htSha1: Result := 'SHA-1';
htSha2_224: Result := 'SHA-2 224';
htSha2_256: Result := 'SHA-2 256';
htSha2_384: Result := 'SHA-2 384';
htSha2_512: Result := 'SHA-2 512';
htSha2_512_224: Result := 'SHA-2 512/224';
htSha2_512_256: Result := 'SHA-2 512/256';
htSha3_224: Result := 'SHA-3 224';
htSha3_256: Result := 'SHA-3 256';
htSha3_384: Result := 'SHA-3 384';
htSha3_512: Result := 'SHA-3 512';
htShake128: Result := 'SHAKE-128';
htShake256: Result := 'SHAKE-256';
htWhirlpool: Result := 'Whirlpool';
htSnefru128: Result := 'SNEFRU-128';
htSnefru256: Result := 'SNEFRU-256';
htEdonr256: Result := 'EDON-R 256';
htEdonr512: Result := 'EDON-R 512';
htGost: Result := 'GOST';
htGostCryptopro: Result := 'GOST-CRYPTOPRO';
htHas160: Result := 'HAS-160';
htTiger: Result := 'Tiger';
//htTTH: Result := 'TTH';
//htBTIH: Result := 'BTIH';
htED2K: Result := 'ED2K';
//htAICH: Result := 'AICH';
htHaval: Result := 'HAVAL';
else
Result := StrUnknownHash;
end;
end;
function GetHashStrLength(HashType: TJPHashType; ErrorResult: integer = -1): integer;
begin
case HashType of
htCrc16: Result := HASH_LEN_CRC16;
htCrc24: Result := HASH_LEN_CRC24;
htCrc32: Result := HASH_LEN_CRC32;
htCrc64: Result := HASH_LEN_CRC64;
htAdler32: Result := HASH_LEN_ADLER32;
htMd2: Result := HASH_LEN_MD2;
htMd4: Result := HASH_LEN_MD4;
htMd5: Result := HASH_LEN_MD5;
htRipeMD: Result := HASH_LEN_RIPEMD;
htRipeMD128: Result := HASH_LEN_RIPEMD128;
htRipeMD160: Result := HASH_LEN_RIPEMD160;
htRipeMD256: Result := HASH_LEN_RIPEMD256;
htRipeMD320: Result := HASH_LEN_RIPEMD320;
htSha0: Result := HASH_LEN_SHA0;
htSha1: Result := HASH_LEN_SHA1;
htSha2_224: Result := HASH_LEN_SHA2_224;
htSha2_256: Result := HASH_LEN_SHA2_256;
htSha2_384: Result := HASH_LEN_SHA2_384;
htSha2_512: Result := HASH_LEN_SHA2_512;
htSha2_512_224: Result := HASH_LEN_SHA2_512_224;
htSha2_512_256: Result := HASH_LEN_SHA2_512_256;
htSha3_224: Result := HASH_LEN_SHA3_224;
htSha3_256: Result := HASH_LEN_SHA3_256;
htSha3_384: Result := HASH_LEN_SHA3_384;
htSha3_512: Result := HASH_LEN_SHA3_512;
htShake128: Result := HASH_LEN_SHAKE128;
htShake256: Result := HASH_LEN_SHAKE256;
htWhirlpool: Result := HASH_LEN_WHIRLPOOL;
htSnefru128: Result := HASH_LEN_SNEFRU128;
htSnefru256: Result := HASH_LEN_SNEFRU256;
htEdonr256: Result := HASH_LEN_EDONR256;
htEdonr512: Result := HASH_LEN_EDONR512;
htGost: Result := HASH_LEN_GOST;
htGostCryptopro: Result := HASH_LEN_GOST_CRYPTOPRO;
htHas160: Result := HASH_LEN_HAS160;
htTiger: Result := HASH_LEN_TIGER;
//htTTH: Result := HASH_LEN_TTH;
//htBTIH: Result := HASH_LEN_BTIH;
htED2K: Result := HASH_LEN_ED2K;
//htAICH: Result := HASH_LEN_AICH;
htHaval: Result := HASH_LEN_HAVAL;
else
Result := ErrorResult;
end;
end;
function GetHashDigestLength(const HashType: TJPHashType; ErrorResult: integer = -1): integer;
var
StrLen: integer;
begin
Result := ErrorResult;
StrLen := GetHashStrLength(HashType, ErrorResult);
if StrLen = ErrorResult then Exit;
Result := StrLen div 2;
end;
function GetHashByteLength(const HashType: TJPHashType; ErrorResult: integer = -1): integer;
begin
Result := GetHashDigestLength(HashType, ErrorResult);
end;
function GetHashBitLength(const HashType: TJPHashType; ErrorResult: integer = -1): integer;
var
x: integer;
begin
Result := ErrorResult;
x := GetHashDigestLength(HashType, ErrorResult);
if x = ErrorResult then Exit;
Result := x * 8;
end;
function IsValidHashLen(const Hash: string; HashType: TJPHashType): Boolean;
var
HashLen: integer;
begin
HashLen := Length(Hash);
if Odd(HashLen) then Result := False
else Result := HashLen = GetHashStrLength(HashType, -1);
end;
function IsValidHashStr(const Hash: string): Boolean;
begin
Result := IsValidHexStr(Hash, False);
end;
function CheckHash(Hash: string; HashType: TJPHashType; var HashCheckRec: THashCheckRec): Boolean;
begin
HashCheckRec.ValidChars := IsValidHashStr(Hash);
HashCheckRec.ValidLen := IsValidHashLen(Hash, HashType);
HashCheckRec.ExpectedLen := GetHashStrLength(HashType);
Result := HashCheckRec.ValidChars and HashCheckRec.ValidLen;
end;
function FormatHash(HashStr: string; bUpperCase: Boolean; ByteSeparator: string = ''): string;
var
i: integer;
s: string;
begin
s := HashStr;
if bUpperCase then s := UpperCase(s)
else s := LowerCase(s);
if ByteSeparator <> '' then
for i := 1 to Length(s) do
begin
if i mod 2 = 0 then
begin
end;
end;
Result := s;
end;
{ THashResultRec }
procedure THashResultRec.Clear;
begin
ValidHash := False;
HashType := htNone;
StrValueUpper := '';
StrValueLower := '';
IntValue := 0;
Int64Value := 0;
StreamSize := 0;
ElapsedTimeMs := 0;
SpeedMBperSec := 0;
end;
end.