-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSHA2.pas
3889 lines (3262 loc) · 131 KB
/
SHA2.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
SHA-2 calculation
SHA-2 consists of two groups of hashes. First group is calculated using
32 bit long words and contains SHA-224 and SHA-256. The other group is
calculated using 64 bit long words and contains all other hashes.
Both groups are completely implemented, therefore following hashes are
implemented and supported by this library:
SHA-224
SHA-256
SHA-384
SHA-512
SHA-512/224
SHA-512/256
Inheritance tree of hash classes looks like this (star marks an abstract
class that must not be directy instantiated):
*TBlockHash --- *TSHA2Hash --- *TSHA2Hash_32 --- TSHA224Hash
| |- TSHA256Hash
|
|- *TSHA2Hash_64 --- TSHA384Hash
|- TSHA512Hash
|- TSHA512_224Hash
|- TSHA512_256Hash
Given the nature of calculation, 64bit-based hashes performs poorly on
32bit systems. On the other hand, they seem to give better performance on
64bit systems than 32bit-based hashes.
Version 1.1.3 (2020-07-13)
Last change 2024-03-05
©2015-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.SHA2
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
BitOps - github.com/TheLazyTomcat/Lib.BitOps
HashBase - github.com/TheLazyTomcat/Lib.HashBase
StrRect - github.com/TheLazyTomcat/Lib.StrRect
Indirect dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit SHA2;
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$INLINE ON}
{$DEFINE CanInline}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ELSE}
{$IF CompilerVersion >= 17} // Delphi 2005+
{$DEFINE CanInline}
{$ELSE}
{$UNDEF CanInline}
{$IFEND}
{$ENDIF}
{$H+}
{$IFOPT Q+}
{$DEFINE OverflowChecks}
{$ENDIF}
interface
uses
Classes,
AuxTypes, HashBase;
{===============================================================================
Auxiliary types, constants and functions
===============================================================================}
type
UInt128 = packed record
case Integer of
0:(Lo,Hi: UInt64);
1:(Bytes: packed array[0..15] of UInt8);
2:(Words: packed array[0..7] of UInt16);
3:(DWords: packed array[0..3] of UInt32);
4:(QWords: packed array[0..1] of UInt64);
end;
PUInt128 = ^UInt128;
OctaWord = UInt128;
POctaWord = ^OctaWord;
OWord = OctaWord;
POWord = ^OWord;
const
ZeroUInt128: UInt128 = (Lo: 0; Hi: 0);
Function BuildOctaWord(Lo,Hi: UInt64): UInt128; overload;
Function BuildOctaWord(Lo: UInt64): UInt128; overload;{$IFDEF CanInline} inline; {$ENDIF}
procedure IncrementOctaWord(var Value: UInt128; Increment: UInt128);
Function SizeToMessageLength(Size: UInt64): UInt128;
Function MessageLengthToSize(MessageLength: UInt128): UInt64;
Function EndianSwap(Value: UInt128): UInt128; overload;
procedure EndianSwapValue(var Value: UInt128); overload;{$IFDEF CanInline} inline; {$ENDIF}
{===============================================================================
Common types and constants
===============================================================================}
{
Bytes in types TSHA224 trough TSHA512_256 are always ordered from the most
significant byte to least significant byte (big endian).
Types TSHA*Sys has no such guarantee and their internal structure depends on
current implementation.
SHA-2 does not differ in little and big endian form, as it is not a single
quantity, therefore methods like SHA*ToLE or SHA*ToBE do nothing and are
present only for the sake of completeness.
}
type
TSHA2_32 = packed array[0..31] of UInt8; PSHA2_32 = ^TSHA2_32;
TSHA224 = type TSHA2_32; PSHA224 = ^TSHA224;
TSHA256 = type TSHA2_32; PSHA256 = ^TSHA256;
TSHA2_64 = packed array[0..63] of UInt8; PSHA2_64 = ^TSHA2_64;
TSHA384 = type TSHA2_64; PSHA384 = ^TSHA384;
TSHA512 = type TSHA2_64; PSHA512 = ^TSHA512;
TSHA512_224 = type TSHA2_64; PSHA512_224 = ^TSHA512_224;
TSHA512_256 = type TSHA2_64; PSHA512_256 = ^TSHA512_256;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TSHA2Function = (fnSHA224,fnSHA256,fnSHA384,fnSHA512,fnSHA512_224,fnSHA512_256);
TSHA2 = record
case HashFunction: TSHA2Function of
fnSHA224: (SHA224: TSHA224);
fnSHA256: (SHA256: TSHA256);
fnSHA384: (SHA384: TSHA384);
fnSHA512: (SHA512: TSHA512);
fnSHA512_224: (SHA512_224: TSHA512_224);
fnSHA512_256: (SHA512_256: TSHA512_256);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TSHA2Sys_32 = packed record
PartA: UInt32;
PartB: UInt32;
PartC: UInt32;
PartD: UInt32;
PartE: UInt32;
PartF: UInt32;
PartG: UInt32;
PartH: UInt32;
end;
PSHA2Sys_32 = ^TSHA2Sys_32;
TSHA224Sys = type TSHA2Sys_32; PSHA224Sys = ^TSHA224Sys;
TSHA256Sys = type TSHA2Sys_32; PSHA256Sys = ^TSHA256Sys;
TSHA2Sys_64 = packed record
PartA: UInt64;
PartB: UInt64;
PartC: UInt64;
PartD: UInt64;
PartE: UInt64;
PartF: UInt64;
PartG: UInt64;
PartH: UInt64;
end;
PSHA2Sys_64 = ^TSHA2Sys_64;
TSHA384Sys = type TSHA2Sys_64; PSHA384Sys = ^TSHA384Sys;
TSHA512Sys = type TSHA2Sys_64; PSHA512Sys = ^TSHA512Sys;
TSHA512_224Sys = type TSHA2Sys_64; PSHA512_224Sys = ^TSHA512_224Sys;
TSHA512_256Sys = type TSHA2Sys_64; PSHA512_256Sys = ^TSHA512_256Sys;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const
InitialSHA224: TSHA224 =
($C1,$05,$9E,$D8,$36,$7C,$D5,$07,$30,$70,$DD,$17,$F7,$0E,$59,$39,
$FF,$C0,$0B,$31,$68,$58,$15,$11,$64,$F9,$8F,$A7,$BE,$FA,$4F,$A4);
InitialSHA256: TSHA256 =
($6A,$09,$E6,$67,$BB,$67,$AE,$85,$3C,$6E,$f3,$72,$A5,$4f,$f5,$3A,
$51,$0E,$52,$7f,$9B,$05,$68,$8C,$1F,$83,$d9,$AB,$5B,$E0,$CD,$19);
InitialSHA384: TSHA384 =
($CB,$BB,$9D,$5D,$C1,$05,$9E,$D8,$62,$9A,$29,$2A,$36,$7C,$D5,$07,
$91,$59,$01,$5A,$30,$70,$DD,$17,$15,$2F,$EC,$D8,$F7,$0E,$59,$39,
$67,$33,$26,$67,$FF,$C0,$0B,$31,$8E,$B4,$4A,$87,$68,$58,$15,$11,
$DB,$0C,$2E,$0D,$64,$F9,$8F,$A7,$47,$B5,$48,$1D,$BE,$FA,$4F,$A4);
InitialSHA512: TSHA512 =
($6A,$09,$E6,$67,$F3,$BC,$C9,$08,$BB,$67,$AE,$85,$84,$CA,$A7,$3B,
$3C,$6E,$F3,$72,$FE,$94,$F8,$2B,$A5,$4F,$F5,$3A,$5F,$1D,$36,$F1,
$51,$0E,$52,$7F,$AD,$E6,$82,$D1,$9B,$05,$68,$8C,$2B,$3E,$6C,$1F,
$1F,$83,$D9,$AB,$FB,$41,$BD,$6B,$5B,$E0,$CD,$19,$13,$7E,$21,$79);
InitialSHA512Mod: TSHA512 =
($CF,$AC,$43,$C2,$56,$19,$6C,$AD,$1E,$C2,$0B,$20,$21,$6F,$02,$9E,
$99,$CB,$56,$D7,$5B,$31,$5D,$8E,$00,$EA,$50,$9F,$FA,$B8,$93,$54,
$F4,$AB,$F7,$DA,$08,$43,$27,$74,$3E,$A0,$CD,$29,$8E,$9B,$C9,$BA,
$BA,$26,$7C,$0E,$5E,$E4,$18,$CE,$FE,$45,$68,$BC,$B6,$DB,$84,$DC);
{
InitialSHA512_224 is calculated as a SHA512 of ASCII string 'SHA-512/224'
(without quotes) with initial value being the InitialSHA512Mod.
}
InitialSHA512_224: TSHA512_224 =
($8C,$3D,$37,$C8,$19,$54,$4D,$A2,$73,$E1,$99,$66,$89,$DC,$D4,$D6,
$1D,$FA,$B7,$AE,$32,$FF,$9C,$82,$67,$9D,$D5,$14,$58,$2F,$9F,$CF,
$0F,$6D,$2B,$69,$7B,$D4,$4D,$A8,$77,$E3,$6F,$73,$04,$C4,$89,$42,
$3F,$9D,$85,$A8,$6A,$1D,$36,$C8,$11,$12,$E6,$AD,$91,$D6,$92,$A1);
{
InitialSHA512_256 is calculated as a SHA512 of ASCII string 'SHA-512/256'
(without quotes) with initial value being the InitialSHA512Mod.
}
InitialSHA512_256: TSHA512_256 =
($22,$31,$21,$94,$FC,$2B,$F7,$2C,$9F,$55,$5F,$A3,$C8,$4C,$64,$C2,
$23,$93,$B8,$6B,$6F,$53,$B1,$51,$96,$38,$77,$19,$59,$40,$EA,$BD,
$96,$28,$3E,$E2,$A8,$8E,$FF,$E3,$BE,$5E,$1E,$25,$53,$86,$39,$92,
$2B,$01,$99,$FC,$2C,$85,$B8,$AA,$0E,$B7,$2D,$DC,$81,$C5,$2C,$A2);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ZeroSHA224: TSHA224 = (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);
ZeroSHA256: TSHA256 = (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);
ZeroSHA384: TSHA384 = (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,0);
ZeroSHA512: TSHA512 = (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,0);
ZeroSHA512_224: TSHA512_224 = (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,0);
ZeroSHA512_256: TSHA512_256 = (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,0);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type
ESHA2Exception = class(EHashException);
ESHA2IncompatibleClass = class(ESHA2Exception);
ESHA2IncompatibleFunction = class(ESHA2Exception);
ESHA2ProcessingError = class(ESHA2Exception);
ESHA2InvalidFunction = class(ESHA2Exception);
{-------------------------------------------------------------------------------
================================================================================
TSHA2Hash
================================================================================
-------------------------------------------------------------------------------}
type
TSHA2HashBuffer = packed array[0..64] of UInt8;
{===============================================================================
TSHA2Hash - class declaration
===============================================================================}
type
TSHA2Hash = class(TBlockHash)
protected
class Function HashBufferToLE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer; virtual;
class Function HashBufferToBE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer; virtual;
class Function HashBufferFromLE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer; virtual;
class Function HashBufferFromBE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer; virtual;
Function GetHashBuffer: TSHA2HashBuffer; virtual; abstract;
procedure SetHashBuffer(HashBuffer: TSHA2HashBuffer); virtual; abstract;
Function GetSHA2: TSHA2; virtual;
procedure ProcessFirst(const Block); override;
public
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
class Function HashFunction: TSHA2Function; virtual; abstract;
{
Since HashSize returns technical size of the hash, which is not always the
same as is indicated by its name, observed size was introduced. It returns
number of bytes that are observed in the hash (they are accessed when
comparing and converting to/from string).
}
class Function HashObservedSize: TMemSize; virtual; abstract;
constructor CreateAndInitFrom(Hash: TSHA2); overload; virtual; abstract;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TSHA2); reintroduce; overload; virtual;
{
Note that, by default, the hash is streamed in its entirety, not only
observed bytes. How many bytes will be written/read is equal to HashSize.
If you want to only stream observed bytes, use methods SaveObservedToStream
and LoadObservedFromStream.
Note that since setting endianness does not change the streamed data, it
is omitted from observed streaming.
}
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure SaveObservedToStream(Stream: TStream); virtual;
procedure LoadObservedFromStream(Stream: TStream); virtual;
property SHA2: TSHA2 read GetSHA2;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA2Hash_32
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA2Hash_32 - class declaration
===============================================================================}
type
TSHA2Hash_32 = class(TSHA2Hash)
protected
fSHA2: TSHA2Sys_32;
procedure ProcessBlock(const Block); override;
procedure ProcessLast; override;
procedure Initialize; override;
public
property SHA2Sys: TSHA2Sys_32 read fSHA2;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA2Hash_64
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA2Hash_64 - class declaration
===============================================================================}
type
TSHA2Hash_64 = class(TSHA2Hash)
protected
fSHA2: TSHA2Sys_64;
procedure ProcessBlock(const Block); override;
procedure ProcessLast; override;
procedure Initialize; override;
public
property SHA2Sys: TSHA2Sys_64 read fSHA2;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA224Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA224Hash - class declaration
===============================================================================}
type
TSHA224Hash = class(TSHA2Hash_32)
protected
Function GetHashBuffer: TSHA2HashBuffer; override;
procedure SetHashBuffer(HashBuffer: TSHA2HashBuffer); override;
Function GetSHA224: TSHA224; virtual;
Function GetSHA224Sys: TSHA224Sys; virtual;
procedure Initialize; override;
public
class Function SHA224ToSys(SHA224: TSHA224): TSHA224Sys; virtual;
class Function SHA224FromSys(SHA224: TSHA224Sys): TSHA224; virtual;
class Function SHA224ToLE(SHA224: TSHA224): TSHA224; virtual;
class Function SHA224ToBE(SHA224: TSHA224): TSHA224; virtual;
class Function SHA224FromLE(SHA224: TSHA224): TSHA224; virtual;
class Function SHA224FromBE(SHA224: TSHA224): TSHA224; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
class Function HashFunction: TSHA2Function; override;
class Function HashObservedSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA2); overload; override;
constructor CreateAndInitFrom(Hash: TSHA224); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA2); overload; override;
procedure FromStringDef(const Str: String; const Default: TSHA224); overload; virtual;
property SHA224: TSHA224 read GetSHA224;
property SHA224Sys: TSHA224Sys read GetSHA224Sys;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA256Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA256Hash - class declaration
===============================================================================}
type
TSHA256Hash = class(TSHA2Hash_32)
protected
Function GetHashBuffer: TSHA2HashBuffer; override;
procedure SetHashBuffer(HashBuffer: TSHA2HashBuffer); override;
Function GetSHA256: TSHA256; virtual;
Function GetSHA256Sys: TSHA256Sys; virtual;
procedure Initialize; override;
public
class Function SHA256ToSys(SHA256: TSHA256): TSHA256Sys; virtual;
class Function SHA256FromSys(SHA256: TSHA256Sys): TSHA256; virtual;
class Function SHA256ToLE(SHA256: TSHA256): TSHA256; virtual;
class Function SHA256ToBE(SHA256: TSHA256): TSHA256; virtual;
class Function SHA256FromLE(SHA256: TSHA256): TSHA256; virtual;
class Function SHA256FromBE(SHA256: TSHA256): TSHA256; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
class Function HashFunction: TSHA2Function; override;
class Function HashObservedSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA2); overload; override;
constructor CreateAndInitFrom(Hash: TSHA256); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA2); overload; override;
procedure FromStringDef(const Str: String; const Default: TSHA256); overload; virtual;
property SHA256: TSHA256 read GetSHA256;
property SHA256Sys: TSHA256Sys read GetSHA256Sys;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA384Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA384Hash - class declaration
===============================================================================}
type
TSHA384Hash = class(TSHA2Hash_64)
protected
Function GetHashBuffer: TSHA2HashBuffer; override;
procedure SetHashBuffer(HashBuffer: TSHA2HashBuffer); override;
Function GetSHA384: TSHA384; virtual;
Function GetSHA384Sys: TSHA384Sys; virtual;
procedure Initialize; override;
public
class Function SHA384ToSys(SHA384: TSHA384): TSHA384Sys; virtual;
class Function SHA384FromSys(SHA384: TSHA384Sys): TSHA384; virtual;
class Function SHA384ToLE(SHA384: TSHA384): TSHA384; virtual;
class Function SHA384ToBE(SHA384: TSHA384): TSHA384; virtual;
class Function SHA384FromLE(SHA384: TSHA384): TSHA384; virtual;
class Function SHA384FromBE(SHA384: TSHA384): TSHA384; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
class Function HashFunction: TSHA2Function; override;
class Function HashObservedSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA2); overload; override;
constructor CreateAndInitFrom(Hash: TSHA384); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA2); overload; override;
procedure FromStringDef(const Str: String; const Default: TSHA384); overload; virtual;
property SHA384: TSHA384 read GetSHA384;
property SHA384Sys: TSHA384Sys read GetSHA384Sys;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA512Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA512Hash - class declaration
===============================================================================}
type
TSHA512Hash = class(TSHA2Hash_64)
protected
Function GetHashBuffer: TSHA2HashBuffer; override;
procedure SetHashBuffer(HashBuffer: TSHA2HashBuffer); override;
Function GetSHA512: TSHA512; virtual;
Function GetSHA512Sys: TSHA512Sys; virtual;
procedure Initialize; override;
public
class Function SHA512ToSys(SHA512: TSHA512): TSHA512Sys; virtual;
class Function SHA512FromSys(SHA512: TSHA512Sys): TSHA512; virtual;
class Function SHA512ToLE(SHA512: TSHA512): TSHA512; virtual;
class Function SHA512ToBE(SHA512: TSHA512): TSHA512; virtual;
class Function SHA512FromLE(SHA512: TSHA512): TSHA512; virtual;
class Function SHA512FromBE(SHA512: TSHA512): TSHA512; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
class Function HashFunction: TSHA2Function; override;
class Function HashObservedSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA2); overload; override;
constructor CreateAndInitFrom(Hash: TSHA512); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA2); overload; override;
procedure FromStringDef(const Str: String; const Default: TSHA512); overload; virtual;
property SHA512: TSHA512 read GetSHA512;
property SHA512Sys: TSHA512Sys read GetSHA512Sys;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA512_224Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA512_224Hash - class declaration
===============================================================================}
type
TSHA512_224Hash = class(TSHA2Hash_64)
protected
Function GetHashBuffer: TSHA2HashBuffer; override;
procedure SetHashBuffer(HashBuffer: TSHA2HashBuffer); override;
Function GetSHA512_224: TSHA512_224; virtual;
Function GetSHA512_224Sys: TSHA512_224Sys; virtual;
procedure Initialize; override;
public
class Function SHA512_224ToSys(SHA512_224: TSHA512_224): TSHA512_224Sys; virtual;
class Function SHA512_224FromSys(SHA512_224: TSHA512_224Sys): TSHA512_224; virtual;
class Function SHA512_224ToLE(SHA512_224: TSHA512_224): TSHA512_224; virtual;
class Function SHA512_224ToBE(SHA512_224: TSHA512_224): TSHA512_224; virtual;
class Function SHA512_224FromLE(SHA512_224: TSHA512_224): TSHA512_224; virtual;
class Function SHA512_224FromBE(SHA512_224: TSHA512_224): TSHA512_224; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
class Function HashFunction: TSHA2Function; override;
class Function HashObservedSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA2); overload; override;
constructor CreateAndInitFrom(Hash: TSHA512_224); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA2); overload; override;
procedure FromStringDef(const Str: String; const Default: TSHA512_224); overload; virtual;
property SHA512_224: TSHA512_224 read GetSHA512_224;
property SHA512_224Sys: TSHA512_224Sys read GetSHA512_224Sys;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA512_256Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA512_256Hash - class declaration
===============================================================================}
type
TSHA512_256Hash = class(TSHA2Hash_64)
protected
Function GetHashBuffer: TSHA2HashBuffer; override;
procedure SetHashBuffer(HashBuffer: TSHA2HashBuffer); override;
Function GetSHA512_256: TSHA512_256; virtual;
Function GetSHA512_256Sys: TSHA512_256Sys; virtual;
procedure Initialize; override;
public
class Function SHA512_256ToSys(SHA512_256: TSHA512_256): TSHA512_256Sys; virtual;
class Function SHA512_256FromSys(SHA512_256: TSHA512_256Sys): TSHA512_256; virtual;
class Function SHA512_256ToLE(SHA512_256: TSHA512_256): TSHA512_256; virtual;
class Function SHA512_256ToBE(SHA512_256: TSHA512_256): TSHA512_256; virtual;
class Function SHA512_256FromLE(SHA512_256: TSHA512_256): TSHA512_256; virtual;
class Function SHA512_256FromBE(SHA512_256: TSHA512_256): TSHA512_256; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
class Function HashFunction: TSHA2Function; override;
class Function HashObservedSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA2); overload; override;
constructor CreateAndInitFrom(Hash: TSHA512_256); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA2); overload; override;
procedure FromStringDef(const Str: String; const Default: TSHA512_256); overload; virtual;
property SHA512_256: TSHA512_256 read GetSHA512_256;
property SHA512_256Sys: TSHA512_256Sys read GetSHA512_256Sys;
end;
{===============================================================================
Auxiliary functions
===============================================================================}
Function InitialSHA2(HashFunction: TSHA2Function): TSHA2;
Function CreateByFunction(HashFunction: TSHA2Function): TSHA2Hash;
Function CreateFromByFunction(HashFunction: TSHA2Function; Hash: TSHA2Hash): TSHA2Hash; overload;
Function CreateFromByFunction(HashFunction: TSHA2Function; Hash: TSHA2): TSHA2Hash; overload;
Function CreateFromByFunction(SHA2: TSHA2): TSHA2Hash; overload;{$IFDEF CanInline} inline; {$ENDIF}
{===============================================================================
Backward compatibility functions
===============================================================================}
{
Following two functions could be implemented to only return precomputed
constants, but for the sake of backward compatibility, they are actually
calculating the result.
}
Function InitialSHA2_512_224: TSHA512_224;
Function InitialSHA2_512_256: TSHA512_256;
//------------------------------------------------------------------------------
Function SHA2ToStr(SHA224: TSHA224): String; overload;
Function SHA2ToStr(SHA256: TSHA256): String; overload;
Function SHA2ToStr(SHA384: TSHA384): String; overload;
Function SHA2ToStr(SHA512: TSHA512): String; overload;
Function SHA2ToStr(SHA512_224: TSHA512_224): String; overload;
Function SHA2ToStr(SHA512_256: TSHA512_256): String; overload;
Function SHA2ToStr(SHA2: TSHA2): String; overload;
{
Delphi cannot overload based on result type, welp...
}
Function StrToSHA2_224(Str: String): TSHA224;
Function StrToSHA2_256(Str: String): TSHA256;
Function StrToSHA2_384(Str: String): TSHA384;
Function StrToSHA2_512(Str: String): TSHA512;
Function StrToSHA2_512_224(Str: String): TSHA512_224;
Function StrToSHA2_512_256(Str: String): TSHA512_256;
Function StrToSHA2(HashFunction: TSHA2Function; Str: String): TSHA2;
Function TryStrToSHA2(const Str: String; out SHA224: TSHA224): Boolean; overload;
Function TryStrToSHA2(const Str: String; out SHA256: TSHA256): Boolean; overload;
Function TryStrToSHA2(const Str: String; out SHA384: TSHA384): Boolean; overload;
Function TryStrToSHA2(const Str: String; out SHA512: TSHA512): Boolean; overload;
Function TryStrToSHA2(const Str: String; out SHA512_224: TSHA512_224): Boolean; overload;
Function TryStrToSHA2(const Str: String; out SHA512_256: TSHA512_256): Boolean; overload;
Function TryStrToSHA2(HashFunction: TSHA2Function; const Str: String; out SHA2: TSHA2): Boolean; overload;
Function StrToSHA2Def(const Str: String; Default: TSHA224): TSHA224; overload;
Function StrToSHA2Def(const Str: String; Default: TSHA256): TSHA256; overload;
Function StrToSHA2Def(const Str: String; Default: TSHA384): TSHA384; overload;
Function StrToSHA2Def(const Str: String; Default: TSHA512): TSHA512; overload;
Function StrToSHA2Def(const Str: String; Default: TSHA512_224): TSHA512_224; overload;
Function StrToSHA2Def(const Str: String; Default: TSHA512_256): TSHA512_256; overload;
Function StrToSHA2Def(HashFunction: TSHA2Function; const Str: String; Default: TSHA2): TSHA2; overload;
Function CompareSHA2(A,B: TSHA224): Integer; overload;
Function CompareSHA2(A,B: TSHA256): Integer; overload;
Function CompareSHA2(A,B: TSHA384): Integer; overload;
Function CompareSHA2(A,B: TSHA512): Integer; overload;
Function CompareSHA2(A,B: TSHA512_224): Integer; overload;
Function CompareSHA2(A,B: TSHA512_256): Integer; overload;
Function CompareSHA2(A,B: TSHA2): Integer; overload;
Function SameSHA2(A,B: TSHA224): Boolean; overload;
Function SameSHA2(A,B: TSHA256): Boolean; overload;
Function SameSHA2(A,B: TSHA384): Boolean; overload;
Function SameSHA2(A,B: TSHA512): Boolean; overload;
Function SameSHA2(A,B: TSHA512_224): Boolean; overload;
Function SameSHA2(A,B: TSHA512_256): Boolean; overload;
Function SameSHA2(A,B: TSHA2): Boolean; overload;
Function BinaryCorrectSHA2(SHA224: TSHA224): TSHA224; overload;{$IFDEF CanInline} inline; {$ENDIF}
Function BinaryCorrectSHA2(SHA256: TSHA256): TSHA256; overload;{$IFDEF CanInline} inline; {$ENDIF}
Function BinaryCorrectSHA2(SHA384: TSHA384): TSHA384; overload;{$IFDEF CanInline} inline; {$ENDIF}
Function BinaryCorrectSHA2(SHA512: TSHA512): TSHA512; overload;{$IFDEF CanInline} inline; {$ENDIF}
Function BinaryCorrectSHA2(SHA512_224: TSHA512_224): TSHA512_224; overload;{$IFDEF CanInline} inline; {$ENDIF}
Function BinaryCorrectSHA2(SHA512_256: TSHA512_256): TSHA512_256; overload;{$IFDEF CanInline} inline; {$ENDIF}
Function BinaryCorrectSHA2(SHA2: TSHA2): TSHA2; overload;{$IFDEF CanInline} inline; {$ENDIF}
//------------------------------------------------------------------------------
procedure BufferSHA2(var SHA224: TSHA224; const Buffer; Size: TMemSize); overload;
procedure BufferSHA2(var SHA256: TSHA256; const Buffer; Size: TMemSize); overload;
procedure BufferSHA2(var SHA384: TSHA384; const Buffer; Size: TMemSize); overload;
procedure BufferSHA2(var SHA512: TSHA512; const Buffer; Size: TMemSize); overload;
procedure BufferSHA2(var SHA512_224: TSHA512_224; const Buffer; Size: TMemSize); overload;
procedure BufferSHA2(var SHA512_256: TSHA512_256; const Buffer; Size: TMemSize); overload;
procedure BufferSHA2(var SHA2: TSHA2; const Buffer; Size: TMemSize); overload;
Function LastBufferSHA2(SHA224: TSHA224; const Buffer; Size: TMemSize; MessageLength: UInt64): TSHA224; overload;
Function LastBufferSHA2(SHA256: TSHA256; const Buffer; Size: TMemSize; MessageLength: UInt64): TSHA256; overload;
Function LastBufferSHA2(SHA384: TSHA384; const Buffer; Size: TMemSize; MessageLength: UInt128): TSHA384; overload;
Function LastBufferSHA2(SHA512: TSHA512; const Buffer; Size: TMemSize; MessageLength: UInt128): TSHA512; overload;
Function LastBufferSHA2(SHA512_224: TSHA512_224; const Buffer; Size: TMemSize; MessageLength: UInt128): TSHA512_224; overload;
Function LastBufferSHA2(SHA512_256: TSHA512_256; const Buffer; Size: TMemSize; MessageLength: UInt128): TSHA512_256; overload;
{
Inlining note - FPC at this point (05-2020) does not support untyped (formal)
parameter in inlined functions.
}
Function LastBufferSHA2(SHA384: TSHA384; const Buffer; Size: TMemSize; MessageLengthLo: UInt64): TSHA384; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512: TSHA512; const Buffer; Size: TMemSize; MessageLengthLo: UInt64): TSHA512; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512_224: TSHA512_224; const Buffer; Size: TMemSize; MessageLengthLo: UInt64): TSHA512_224; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512_256: TSHA512_256; const Buffer; Size: TMemSize; MessageLengthLo: UInt64): TSHA512_256; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA384: TSHA384; const Buffer; Size: TMemSize; MessageLengthLo, MessageLengthHi: UInt64): TSHA384; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512: TSHA512; const Buffer; Size: TMemSize; MessageLengthLo, MessageLengthHi: UInt64): TSHA512; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512_224: TSHA512_224; const Buffer; Size: TMemSize; MessageLengthLo, MessageLengthHi: UInt64): TSHA512_224; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512_256: TSHA512_256; const Buffer; Size: TMemSize; MessageLengthLo, MessageLengthHi: UInt64): TSHA512_256; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA2: TSHA2; const Buffer; Size: TMemSize; MessageLength: UInt64): TSHA2; overload;
Function LastBufferSHA2(SHA2: TSHA2; const Buffer; Size: TMemSize; MessageLengthLo, MessageLengthHi: UInt64): TSHA2; overload;
Function LastBufferSHA2(SHA2: TSHA2; const Buffer; Size: TMemSize; MessageLength: OctaWord): TSHA2; overload;
Function LastBufferSHA2(SHA224: TSHA224; const Buffer; Size: TMemSize): TSHA224; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA256: TSHA256; const Buffer; Size: TMemSize): TSHA256; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA384: TSHA384; const Buffer; Size: TMemSize): TSHA384; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512: TSHA512; const Buffer; Size: TMemSize): TSHA512; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512_224: TSHA512_224; const Buffer; Size: TMemSize): TSHA512_224; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA512_256: TSHA512_256; const Buffer; Size: TMemSize): TSHA512_256; overload;{$IF defined(CanInline) and not defined(FPC)} inline; {$IFEND}
Function LastBufferSHA2(SHA2: TSHA2; const Buffer; Size: TMemSize): TSHA2; overload;
//------------------------------------------------------------------------------
Function BufferSHA2(HashFunction: TSHA2Function; const Buffer; Size: TMemSize): TSHA2; overload;
Function AnsiStringSHA2(HashFunction: TSHA2Function; const Str: AnsiString): TSHA2;
Function WideStringSHA2(HashFunction: TSHA2Function; const Str: WideString): TSHA2;
Function StringSHA2(HashFunction: TSHA2Function; const Str: String): TSHA2;
Function StreamSHA2(HashFunction: TSHA2Function; Stream: TStream; Count: Int64 = -1): TSHA2;
Function FileSHA2(HashFunction: TSHA2Function; const FileName: String): TSHA2;
//------------------------------------------------------------------------------
type
TSHA2Context = type Pointer;
Function SHA2_Init(HashFunction: TSHA2Function): TSHA2Context;
procedure SHA2_Update(Context: TSHA2Context; const Buffer; Size: TMemSize);
Function SHA2_Final(var Context: TSHA2Context; const Buffer; Size: TMemSize): TSHA2; overload;
Function SHA2_Final(var Context: TSHA2Context): TSHA2; overload;
Function SHA2_Hash(HashFunction: TSHA2Function; const Buffer; Size: TMemSize): TSHA2;
implementation
uses
SysUtils,
BitOps, StrRect;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W4056:={$WARN 4056 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5057:={$WARN 5057 OFF}} // Local variable "$1" does not seem to be initialized
{$ENDIF}
{===============================================================================
Auxiliary functions - implementation
===============================================================================}
{-------------------------------------------------------------------------------
Auxiliary functions - public functions
-------------------------------------------------------------------------------}
Function BuildOctaWord(Lo,Hi: UInt64): UInt128;
begin
Result.Lo := Lo;
Result.Hi := Hi;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function BuildOctaWord(Lo: UInt64): UInt128;
begin
Result := BuildOctaWord(Lo,0);
end;
//------------------------------------------------------------------------------
procedure IncrementOctaWord(var Value: UInt128; Increment: UInt128);
var
Result: UInt64;
Carry: UInt32;
i: Integer;
begin
Carry := 0;
For i := Low(Value.DWords) to High(Value.DWords) do
begin
Result := UInt64(Carry) + Value.DWords[i] + Increment.DWords[i];
Value.DWords[i] := Int64Rec(Result).Lo;
Carry := Int64Rec(Result).Hi;
end;
end;
//------------------------------------------------------------------------------
Function SizeToMessageLength(Size: UInt64): UInt128;
begin
Result.Hi := UInt64(Size shr 61);
Result.Lo := UInt64(Size shl 3);
end;
//------------------------------------------------------------------------------
Function MessageLengthToSize(MessageLength: UInt128): UInt64;
begin
Result := UInt64(MessageLength.Lo shr 3) or
UInt64(MessageLength.Hi shl 61);
end;
//------------------------------------------------------------------------------
Function EndianSwap(Value: UInt128): UInt128;
begin
Result.Hi := EndianSwap(Value.Lo);
Result.Lo := EndianSwap(Value.Hi);
end;
//------------------------------------------------------------------------------
procedure EndianSwapValue(var Value: UInt128);
begin
Value := EndianSwap(Value);
end;
//------------------------------------------------------------------------------
Function InitialSHA2(HashFunction: TSHA2Function): TSHA2;
begin
Result.HashFunction := HashFunction;
case HashFunction of
fnSHA224: Result.SHA224 := InitialSHA224;
fnSHA256: Result.SHA256 := InitialSHA256;
fnSHA384: Result.SHA384 := InitialSHA384;
fnSHA512: Result.SHA512 := InitialSHA512;
fnSHA512_224: Result.SHA512_224 := InitialSHA512_224;
fnSHA512_256: Result.SHA512_256 := InitialSHA512_256;
else
raise ESHA2InvalidFunction.CreateFmt('InitialSHA2: Invalid hash function (%d)',[Ord(HashFunction)]);
end;
end;
//------------------------------------------------------------------------------
Function CreateByFunction(HashFunction: TSHA2Function): TSHA2Hash;
begin
case HashFunction of
fnSHA224: Result := TSHA224Hash.Create;
fnSHA256: Result := TSHA256Hash.Create;
fnSHA384: Result := TSHA384Hash.Create;
fnSHA512: Result := TSHA512Hash.Create;
fnSHA512_224: Result := TSHA512_224Hash.Create;
fnSHA512_256: Result := TSHA512_256Hash.Create;
else
raise ESHA2InvalidFunction.CreateFmt('CreateByFunction: Invalid hash function (%d)',[Ord(HashFunction)]);
end;
end;
//------------------------------------------------------------------------------
Function CreateFromByFunction(HashFunction: TSHA2Function; Hash: TSHA2Hash): TSHA2Hash;
begin
case HashFunction of
fnSHA224: Result := TSHA224Hash.CreateAndInitFrom(Hash);
fnSHA256: Result := TSHA256Hash.CreateAndInitFrom(Hash);
fnSHA384: Result := TSHA384Hash.CreateAndInitFrom(Hash);
fnSHA512: Result := TSHA512Hash.CreateAndInitFrom(Hash);
fnSHA512_224: Result := TSHA512_224Hash.CreateAndInitFrom(Hash);
fnSHA512_256: Result := TSHA512_256Hash.CreateAndInitFrom(Hash);
else
raise ESHA2InvalidFunction.CreateFmt('CreateFromByFunction(TSHA2Hash): Invalid hash function (%d)',[Ord(HashFunction)]);
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CreateFromByFunction(HashFunction: TSHA2Function; Hash: TSHA2): TSHA2Hash;
begin
case HashFunction of
fnSHA224: Result := TSHA224Hash.CreateAndInitFrom(Hash);
fnSHA256: Result := TSHA256Hash.CreateAndInitFrom(Hash);
fnSHA384: Result := TSHA384Hash.CreateAndInitFrom(Hash);
fnSHA512: Result := TSHA512Hash.CreateAndInitFrom(Hash);
fnSHA512_224: Result := TSHA512_224Hash.CreateAndInitFrom(Hash);
fnSHA512_256: Result := TSHA512_256Hash.CreateAndInitFrom(Hash);
else
raise ESHA2InvalidFunction.CreateFmt('CreateFromByFunction(TSHA2): Invalid hash function (%d)',[Ord(HashFunction)]);
end;
end;
//------------------------------------------------------------------------------
Function CreateFromByFunction(SHA2: TSHA2): TSHA2Hash;
begin
Result := CreateFromByFunction(SHA2.HashFunction,SHA2);
end;
{-------------------------------------------------------------------------------
Auxiliary functions - private functions
-------------------------------------------------------------------------------}
procedure EndianSwapValue(var Value: TSHA2Sys_32); overload;
begin
EndianSwapValue(Value.PartA);
EndianSwapValue(Value.PartB);
EndianSwapValue(Value.PartC);
EndianSwapValue(Value.PartD);
EndianSwapValue(Value.PartE);
EndianSwapValue(Value.PartF);
EndianSwapValue(Value.PartG);
EndianSwapValue(Value.PartH);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure EndianSwapValue(var Value: TSHA2Sys_64); overload;
begin
EndianSwapValue(Value.PartA);
EndianSwapValue(Value.PartB);
EndianSwapValue(Value.PartC);
EndianSwapValue(Value.PartD);
EndianSwapValue(Value.PartE);
EndianSwapValue(Value.PartF);
EndianSwapValue(Value.PartG);
EndianSwapValue(Value.PartH);
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA2Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA2Hash - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSHA2Hash - protected methods
-------------------------------------------------------------------------------}
class Function TSHA2Hash.HashBufferToLE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer;
begin
Result := HashBuffer;
end;
//------------------------------------------------------------------------------
class Function TSHA2Hash.HashBufferToBE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer;
begin
Result := HashBuffer;
end;
//------------------------------------------------------------------------------
class Function TSHA2Hash.HashBufferFromLE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer;
begin
Result := HashBuffer;
end;
//------------------------------------------------------------------------------
class Function TSHA2Hash.HashBufferFromBE(HashBuffer: TSHA2HashBuffer): TSHA2HashBuffer;
begin
Result := HashBuffer;
end;
//------------------------------------------------------------------------------
Function TSHA2Hash.GetSHA2: TSHA2;
var
Temp: TSHA2HashBuffer;
begin
Result.HashFunction := HashFunction;
Temp := GetHashBuffer;
Move(Temp,Result.SHA224,HashSize);
end;
//------------------------------------------------------------------------------
procedure TSHA2Hash.ProcessFirst(const Block);
begin
inherited;
ProcessBlock(Block);
end;
{-------------------------------------------------------------------------------
TSHA2Hash - public methods
-------------------------------------------------------------------------------}
class Function TSHA2Hash.HashEndianness: THashEndianness;
begin
// first byte is most significant