-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFNVHash.pas
More file actions
2876 lines (2430 loc) · 91.2 KB
/
FNVHash.pas
File metadata and controls
2876 lines (2430 loc) · 91.2 KB
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/.
-------------------------------------------------------------------------------}
{===============================================================================
Fowler–Noll–Vo (FNV) hash
This unit provides a mean of computing FNV hash of any provided data. Hash
widths of 32, 64, 128, 256, 512 and 1024 bits are all provided. Also,
algorithms of version 0, 1 and 1a are implemented and can be selected for
each and every width variant.
--- DISCLAIMER ---
Implementation is somewhat naive and the entire thing was written without
access to any relevant documentation (ie. no internet, completely offline).
Only source of information was a Wikipedia page - which is not ideal, to
say the least.
This all means that some things might be wrong or missing. If you find any
problem, please let me know.
Version 1.0 (2026-03-04)
Last change 2026-03-04
©2026 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
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.FNVHash
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
HashBase - github.com/TheLazyTomcat/Lib.HashBase
Indirect dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
ListUtils - github.com/TheLazyTomcat/Lib.ListUtils
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit FNVHash;
{$IF defined(CPU64) or defined(CPU64BITS)}
{$DEFINE CPU64bit}
{$ELSEIF defined(CPU16)}
{$MESSAGE FATAL '16bit CPU not supported'}
{$ELSE}
{$DEFINE CPU32bit}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH ClassicProcVars+}
{$MODESWITCH DuplicateLocals+}
{$ENDIF}
{$H+}
interface
uses
Classes,
AuxTypes, HashBase;
{===============================================================================
Library-specific exceptions
===============================================================================}
type
EFNVException = class(EHashException);
EFNVIncompatibleClass = class(EFNVException);
EFNVInvalidValue = class(EFNVException);
EFNVInvalidState = class(EFNVException);
{===============================================================================
Common types and constants
===============================================================================}
{
Bytes in types TFNVb (where b is bit width) are in memory always ordered from
least significant byte to most significant byte (little endian).
Types TFNVbSys have no such guarantee and their endianness is undefiend (note
they do not necessarily have system endianness, and byte ordering can even be
mixed).
To convert the checksum in default ordering to a required specific ordering,
use methods FNVbToLE for little endian and FNVbToBE for big endian. Note that
these methods are expecting the input value to be in default ordering, if it
is not, the result will be wrong. Be careful when using them.
}
type
TFNV32 = packed array[0..3] of UInt8;
PFNV32 = ^TFNV32;
TFNV64 = packed array[0..7] of UInt8;
PFNV64 = ^TFNV64;
TFNV128 = packed array[0..15] of UInt8;
PFNV128 = ^TFNV128;
TFNV256 = packed array[0..31] of UInt8;
PFNV256 = ^TFNV256;
TFNV512 = packed array[0..63] of UInt8;
PFNV512 = ^TFNV512;
TFNV1024 = packed array[0..127] of UInt8;
PFNV1024 = ^TFNV32;
//------------------------------------------------------------------------------
type
TFNV32Sys = UInt32;
PFNV32Sys = ^TFNV32Sys;
TFNV64Sys = UInt64;
PFNV64Sys = ^TFNV64Sys;
{
There is no point in declaring completely new types for larger hashes, they
would end-up as an array of something anyway.
}
TFNV128Sys = type TFNV128;
PFNV128Sys = ^TFNV128Sys;
TFNV256Sys = type TFNV256;
PFNV256Sys = ^TFNV256Sys;
TFNV512Sys = type TFNV512;
PFNV512Sys = ^TFNV512Sys;
TFNV1024Sys = type TFNV1024;
PFNV1024Sys = ^TFNV1024Sys;
//------------------------------------------------------------------------------
const
ZeroFNV32: TFNV32 = (0,0,0,0);
ZeroFNV64: TFNV64 = (0,0,0,0,0,0,0,0);
ZeroFNV128: TFNV128 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroFNV256: TFNV256 = (
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);
ZeroFNV512: TFNV512 = (
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);
ZeroFNV1024: TFNV1024 = (
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,
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);
//------------------------------------------------------------------------------
{
All following initial values can also be obtained by hashing ASCII-encoded
string "chongo <Landon Curt Noll> /\../\" (without the quotes) using FNV-0
algorithm and corresponding size.
}
const
InitialFNV32: TFNV32 = ($C5,$9D,$1C,$81);
InitialFNV64: TFNV64 = ($25,$23,$22,$84,$E4,$9C,$F2,$CB);
InitialFNV128: TFNV128 = (
$8D,$C5,$95,$62,$75,$21,$B8,$62,$42,$01,$BB,$07,$2E,$27,$62,$6C);
InitialFNV256: TFNV256 = (
$35,$05,$EE,$CA,$C8,$B4,$23,$10,$B3,$BB,$B6,$47,$68,$53,$B1,$C8,
$CC,$76,$E5,$C4,$84,$C3,$98,$2D,$36,$50,$C5,$AA,$BC,$8D,$26,$DD);
InitialFNV512: TFNV512 = (
$D9,$9F,$FE,$4A,$AC,$2A,$98,$AC,$4B,$E3,$56,$5F,$41,$36,$20,$18,
$CE,$E7,$DB,$42,$C9,$9B,$A7,$2E,$F6,$92,$C1,$34,$8A,$F6,$48,$E9,
$21,$0D,$00,$00,$00,$00,$00,$00,$00,$00,$00,$C9,$59,$D0,$87,$AC,
$AC,$90,$99,$30,$0F,$E5,$A1,$DC,$16,$44,$1F,$17,$B1,$B0,$6D,$B8);
InitialFNV1024: TFNV1024 = (
$B3,$90,$EE,$71,$6C,$B1,$F4,$AF,$21,$3B,$A9,$C6,$C9,$8C,$DE,$6B,
$55,$AE,$05,$C0,$6C,$25,$5F,$55,$0A,$51,$34,$27,$80,$73,$6E,$EB,
$D7,$C6,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
$00,$00,$00,$00,$00,$D9,$21,$9A,$DA,$74,$36,$DA,$4E,$F3,$3B,$6C,
$A1,$AD,$FD,$23,$42,$FC,$29,$4B,$B7,$28,$10,$59,$5A,$6D,$E5,$32,
$4D,$CC,$8E,$75,$76,$7A,$5F,$00,$00,$00,$00,$00,$00,$00,$00,$00);
{===============================================================================
--------------------------------------------------------------------------------
TFNVBaseHash
--------------------------------------------------------------------------------
===============================================================================}
type
TFNVHashAlgorithm = (algFNV0,algFNV1,algFNV1a);
{===============================================================================
TFNVBaseHash - class declaration
===============================================================================}
type
TFNVBaseHash = class(TStreamHash)
protected
fHashAlgorithm: TFNVHashAlgorithm;
fProcessBuffer: procedure(const Buffer; Size: TMemSize) of object;
procedure ForceHashAlgorithm(NewValue: TFNVHashAlgorithm); virtual;
procedure SetHashAlgorithm(NewValue: TFNVHashAlgorithm); virtual;
procedure ProcessBuffer_FNV_0(const Buffer; Size: TMemSize); virtual; abstract;
{
Algorithm 1 is the same as 0, only the initial value differs - this is
managed by derived classes in method Init.
}
procedure ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize); virtual; abstract;
procedure ProcessBuffer(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
{
Utilities
All these functions expect the hashes to be in their interchangeable form
(ie. not "sys" form). Size of the hash is obtained by calling class method
HashSize.
}
class procedure HashSwapEndian(var Hash); virtual;
class Function HashCompare(const A,B): Integer; virtual;
class Function HashAsString(const Hash): String; virtual;
class procedure HashFromString(const Str: String; out Hash); virtual;
public
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
Function HashName: String; reintroduce; virtual; // must not be class method here
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
property HashAlgorithm: TFNVHashAlgorithm read fHashAlgorithm write SetHashAlgorithm;
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNV32Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TFNV32Hash - class declaration
===============================================================================}
type
TFNV32Hash = class(TFNVBaseHash)
protected
fFNV32Value: TFNV32Sys;
Function GetFNV32: TFNV32; virtual;
procedure ProcessBuffer_FNV_0(const Buffer; Size: TMemSize); override;
procedure ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
public
class Function FNV32ToSys(Hash: TFNV32): TFNV32Sys; virtual;
class Function FNV32FromSys(Hash: TFNV32Sys): TFNV32; virtual;
class Function FNV32ToLE(Hash: TFNV32): TFNV32; virtual;
class Function FNV32ToBE(Hash: TFNV32): TFNV32; virtual;
class Function FNV32FromLE(Hash: TFNV32): TFNV32; virtual;
class Function FNV32FromBE(Hash: TFNV32): TFNV32; virtual;
class Function HashSize: TMemSize; override;
Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
{
CreateAndInitFrom
Parameter Algorithm is here for situations where one wants to continue
processing from given Hash, but with non-default algorithm - HashAlgorithm
property cannot be changed (would raise an EFNVInvalidState exception)
after Init, which is implicitly called by this constructor. So to allow
for non-def algorithm, it must be selected here.
}
constructor CreateAndInitFrom(Hash: TFNV32; HashAlgorithm: TFNVHashAlgorithm = algFNV1a); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TFNV32); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property FNV32: TFNV32 read GetFNV32;
property FNV32Sys: TFNV32Sys read fFNV32Value;
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNV64Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TFNV64Hash - class declaration
===============================================================================}
type
TFNV64Hash = class(TFNVBaseHash)
protected
fFNV64Value: TFNV64Sys;
Function GetFNV64: TFNV64; virtual;
procedure ProcessBuffer_FNV_0(const Buffer; Size: TMemSize); override;
procedure ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
public
class Function FNV64ToSys(Hash: TFNV64): TFNV64Sys; virtual;
class Function FNV64FromSys(Hash: TFNV64Sys): TFNV64; virtual;
class Function FNV64ToLE(Hash: TFNV64): TFNV64; virtual;
class Function FNV64ToBE(Hash: TFNV64): TFNV64; virtual;
class Function FNV64FromLE(Hash: TFNV64): TFNV64; virtual;
class Function FNV64FromBE(Hash: TFNV64): TFNV64; virtual;
class Function HashSize: TMemSize; override;
Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TFNV64; HashAlgorithm: TFNVHashAlgorithm = algFNV1a); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TFNV64); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property FNV64: TFNV64 read GetFNV64;
property FNV64Sys: TFNV64Sys read fFNV64Value;
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNV128Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TFNV128Hash - class declaration
===============================================================================}
type
TFNV128Hash = class(TFNVBaseHash)
protected
fFNV128Value: TFNV128Sys;
Function GetFNV128: TFNV128; virtual;
procedure ProcessBuffer_FNV_0(const Buffer; Size: TMemSize); override;
procedure ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
public
class Function FNV128ToSys(Hash: TFNV128): TFNV128Sys; virtual;
class Function FNV128FromSys(Hash: TFNV128Sys): TFNV128; virtual;
class Function FNV128ToLE(Hash: TFNV128): TFNV128; virtual;
class Function FNV128ToBE(Hash: TFNV128): TFNV128; virtual;
class Function FNV128FromLE(Hash: TFNV128): TFNV128; virtual;
class Function FNV128FromBE(Hash: TFNV128): TFNV128; virtual;
class Function HashSize: TMemSize; override;
Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TFNV128; HashAlgorithm: TFNVHashAlgorithm = algFNV1a); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TFNV128); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property FNV128: TFNV128 read GetFNV128;
property FNV128Sys: TFNV128Sys read fFNV128Value;
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNV256Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TFNV256Hash - class declaration
===============================================================================}
type
TFNV256Hash = class(TFNVBaseHash)
protected
fFNV256Value: TFNV256Sys;
Function GetFNV256: TFNV256; virtual;
procedure ProcessBuffer_FNV_0(const Buffer; Size: TMemSize); override;
procedure ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
public
class Function FNV256ToSys(Hash: TFNV256): TFNV256Sys; virtual;
class Function FNV256FromSys(Hash: TFNV256Sys): TFNV256; virtual;
class Function FNV256ToLE(Hash: TFNV256): TFNV256; virtual;
class Function FNV256ToBE(Hash: TFNV256): TFNV256; virtual;
class Function FNV256FromLE(Hash: TFNV256): TFNV256; virtual;
class Function FNV256FromBE(Hash: TFNV256): TFNV256; virtual;
class Function HashSize: TMemSize; override;
Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TFNV256; HashAlgorithm: TFNVHashAlgorithm = algFNV1a); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TFNV256); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property FNV256: TFNV256 read GetFNV256;
property FNV256Sys: TFNV256Sys read fFNV256Value;
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNV512Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TFNV512Hash - class declaration
===============================================================================}
type
TFNV512Hash = class(TFNVBaseHash)
protected
fFNV512Value: TFNV512Sys;
Function GetFNV512: TFNV512; virtual;
procedure ProcessBuffer_FNV_0(const Buffer; Size: TMemSize); override;
procedure ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
public
class Function FNV512ToSys(Hash: TFNV512): TFNV512Sys; virtual;
class Function FNV512FromSys(Hash: TFNV512Sys): TFNV512; virtual;
class Function FNV512ToLE(Hash: TFNV512): TFNV512; virtual;
class Function FNV512ToBE(Hash: TFNV512): TFNV512; virtual;
class Function FNV512FromLE(Hash: TFNV512): TFNV512; virtual;
class Function FNV512FromBE(Hash: TFNV512): TFNV512; virtual;
class Function HashSize: TMemSize; override;
Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TFNV512; HashAlgorithm: TFNVHashAlgorithm = algFNV1a); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TFNV512); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property FNV512: TFNV512 read GetFNV512;
property FNV512Sys: TFNV512Sys read fFNV512Value;
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNV1024Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TFNV1024Hash - class declaration
===============================================================================}
type
TFNV1024Hash = class(TFNVBaseHash)
protected
fFNV1024Value: TFNV1024Sys;
Function GetFNV1024: TFNV1024; virtual;
procedure ProcessBuffer_FNV_0(const Buffer; Size: TMemSize); override;
procedure ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
public
class Function FNV1024ToSys(Hash: TFNV1024): TFNV1024Sys; virtual;
class Function FNV1024FromSys(Hash: TFNV1024Sys): TFNV1024; virtual;
class Function FNV1024ToLE(Hash: TFNV1024): TFNV1024; virtual;
class Function FNV1024ToBE(Hash: TFNV1024): TFNV1024; virtual;
class Function FNV1024FromLE(Hash: TFNV1024): TFNV1024; virtual;
class Function FNV1024FromBE(Hash: TFNV1024): TFNV1024; virtual;
class Function HashSize: TMemSize; override;
Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TFNV1024; HashAlgorithm: TFNVHashAlgorithm = algFNV1a); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TFNV1024); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property FNV1024: TFNV1024 read GetFNV1024;
property FNV1024Sys: TFNV1024Sys read fFNV1024Value;
end;
{===============================================================================
--------------------------------------------------------------------------------
Procedural interface
--------------------------------------------------------------------------------
===============================================================================}
{
All implemented functions in procedural interface are only wrappers around
TFNVbHash objects.
Only functions for 32bit and 64bit FNV variants are provided - simply because
I do not expect anyone to use larger variants (or this unit at all, when we
are at it :/). But if any demand arises, I can easily add them.
}
{===============================================================================
Procedural interface - 32bit hash declaration
===============================================================================}
Function FNV32ToStr(Hash: TFNV32): String;
Function StrToFNV32(const Str: String): TFNV32;
Function TryStrToFNV32(const Str: String; out Hash: TFNV32): Boolean;
Function StrToFNV32Def(const Str: String; Default: TFNV32): TFNV32;
Function CompareFNV32(A,B: TFNV32): Integer;
Function SameFNV32(A,B: TFNV32): Boolean;
//------------------------------------------------------------------------------
Function BufferFNV32(Hash: TFNV32; const Buffer; Size: TMemSize; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32; overload;
Function BufferFNV32(const Buffer; Size: TMemSize; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32; overload;
Function AnsiStringFNV32(const Str: AnsiString; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32;
Function WideStringFNV32(const Str: WideString; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32;
Function StringFNV32(const Str: String; Algorithm: TFNVHashAlgorithm = algFNV1a): TFNV32;
Function StreamFNV32(Stream: TStream; Count: Int32 = -1; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32;
Function FileFNV32(const FileName: String; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32;
//------------------------------------------------------------------------------
type
TFNV32Context = type Pointer;
Function FNV32_Init(HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32Context;
procedure FNV32_Update(var Context: TFNV32Context; const Buffer; Size: TMemSize);
Function FNV32_Final(var Context: TFNV32Context; const Buffer; Size: TMemSize): TFNV32; overload;
Function FNV32_Final(var Context: TFNV32Context): TFNV32; overload;
Function FNV32_Hash(const Buffer; Size: TMemSize; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV32;
{===============================================================================
Procedural interface - 64bit hash declaration
===============================================================================}
Function FNV64ToStr(Hash: TFNV64): String;
Function StrToFNV64(const Str: String): TFNV64;
Function TryStrToFNV64(const Str: String; out Hash: TFNV64): Boolean;
Function StrToFNV64Def(const Str: String; Default: TFNV64): TFNV64;
Function CompareFNV64(A,B: TFNV64): Integer;
Function SameFNV64(A,B: TFNV64): Boolean;
//------------------------------------------------------------------------------
Function BufferFNV64(Hash: TFNV64; const Buffer; Size: TMemSize; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64; overload;
Function BufferFNV64(const Buffer; Size: TMemSize; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64; overload;
Function AnsiStringFNV64(const Str: AnsiString; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64;
Function WideStringFNV64(const Str: WideString; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64;
Function StringFNV64(const Str: String; Algorithm: TFNVHashAlgorithm = algFNV1a): TFNV64;
Function StreamFNV64(Stream: TStream; Count: Int64 = -1; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64;
Function FileFNV64(const FileName: String; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64;
//------------------------------------------------------------------------------
type
TFNV64Context = type Pointer;
Function FNV64_Init(HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64Context;
procedure FNV64_Update(var Context: TFNV64Context; const Buffer; Size: TMemSize);
Function FNV64_Final(var Context: TFNV64Context; const Buffer; Size: TMemSize): TFNV64; overload;
Function FNV64_Final(var Context: TFNV64Context): TFNV64; overload;
Function FNV64_Hash(const Buffer; Size: TMemSize; HashAlgorithm: TFNVHashAlgorithm = algFNV1a): TFNV64;
implementation
uses
SysUtils;
{$IFOPT Q+}
{$DEFINE OveflowChecks}
{$ELSE}
{$UNDEF OveflowChecks}
{$ENDIF}
{===============================================================================
Arbitrary-length truncated multiplication
===============================================================================}
{
Following function takes two untyped arguments (A and B) and treats them as
unsigned integers of given size (must be multiple of two for 32bit code and
four for 64bit code) with system endianness, multiplies them and then returns
low-order part of result with given size in output parameter R, truncating
overflow into high-order places.
}
procedure TruncatedMul(const A,B; out R; Size: TMemSize);
type
TCompWord = {$IFDEF CPU64bit}UInt32{$ELSE}UInt16{$ENDIF};
TCompLong = {$IFDEF CPU64bit}UInt64{$ELSE}UInt32{$ENDIF};
TCWArrayOverlay = array[0..Pred(SizeOf(TFNV1024Sys) div SizeOf(TCompWord))] of TCompWord;
var
ArrR: TCWArrayOverlay absolute R;
HighIndex: Integer;
procedure AddSubproduct(Subproduct: TCompLong; WordIndex: Integer);
const
HalfMask = TCompLong(TCompWord(-1));
var
Carry: TCompLong;
begin
Carry := 0;
If Subproduct <> 0 then
repeat
Carry := TCompLong(ArrR[WordIndex]) + (Subproduct and HalfMask) + (Carry and HalfMask);
ArrR[WordIndex] := TCompWord(Carry);
Subproduct := Subproduct shr (SizeOf(TCompWord) * 8);
Carry := Carry shr (SizeOf(TCompWord) * 8);
{$IFDEF ENDIAN_BIG}
Dec(WordIndex);
until ((Carry <= 0) and (Subproduct <= 0)) or (WordIndex < 0);
{$ELSE}
Inc(WordIndex);
until ((Carry <= 0) and (Subproduct <= 0)) or (WordIndex > HighIndex);
{$ENDIF}
end;
var
ArrA: TCWArrayOverlay absolute A;
ArrB: TCWArrayOverlay absolute B;
Index: Integer;
i: Integer;
begin
If (Size < SizeOf(TCompWord)) or (Size > SizeOf(TCWArrayOverlay)) or ((Size and Pred(SizeOf(TCompWord))) <> 0) then
raise EFNVInvalidValue.CreateFmt('TruncatedMul: Invalid size (%d)',[Size]);
HighIndex := Pred(Integer(Size div SizeOf(TCompWord)));
FillChar(Addr(R)^,Size,0);
{$IFDEF ENDIAN_BIG}
For Index := HighIndex downto 0 do
For i := HighIndex downto Index do
AddSubproduct(TCompLong(ArrA[i]) * TCompLong(ArrB[Index + (HighIndex - i)]),Index);
{$ELSE}
For Index := 0 to HighIndex do
For i := 0 to Index do
AddSubproduct(TCompLong(ArrA[i]) * TCompLong(ArrB[Index - i]),Index);
{$ENDIF}
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNVBaseHash
--------------------------------------------------------------------------------
===============================================================================}
type
TFNVHashOverlay = packed array[0..Pred(SizeOf(TFNV1024))] of UInt8;
{===============================================================================
TFNVBaseHash - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TFNVBaseHash - protected methods
-------------------------------------------------------------------------------}
procedure TFNVBaseHash.ForceHashAlgorithm(NewValue: TFNVHashAlgorithm);
begin
fHashAlgorithm := NewValue;
If fHashAlgorithm = algFNV1a then
fProcessBuffer := ProcessBuffer_FNV_1a
else
fProcessBuffer := ProcessBuffer_FNV_0;
end;
//------------------------------------------------------------------------------
procedure TFNVBaseHash.SetHashAlgorithm(NewValue: TFNVHashAlgorithm);
begin
If not Initialized or Finalized then
ForceHashAlgorithm(NewValue)
else
raise EFNVInvalidState.Create('TFNVBaseHash.SetHashAlgorithm: Cannot change algorithm during processing.');
end;
//------------------------------------------------------------------------------
procedure TFNVBaseHash.ProcessBuffer(const Buffer; Size: TMemSize);
begin
fProcessBuffer(Buffer,Size);
end;
//------------------------------------------------------------------------------
procedure TFNVBaseHash.Initialize;
begin
inherited;
fHashAlgorithm := algFNV1a;
fProcessBuffer := ProcessBuffer_FNV_1a
end;
//------------------------------------------------------------------------------
class procedure TFNVBaseHash.HashSwapEndian(var Hash);
var
HashOverlay: TFNVHashOverlay absolute Hash;
HashLength: Integer;
i: Integer;
Temp: UInt8;
begin
// buffer length/size of the hash to reduce number of calls to HashSize
HashLength := HashSize;
For i := 0 to Pred(HashLength div 2) do
begin
Temp := HashOverlay[i];
HashOverlay[i] := HashOverlay[Pred(HashLength) - i];
HashOverlay[Pred(HashLength) - i] := Temp;
end;
end;
//------------------------------------------------------------------------------
class Function TFNVBaseHash.HashCompare(const A,B): Integer;
var
AOverlay: TFNVHashOverlay absolute A;
BOverlay: TFNVHashOverlay absolute B;
i: Integer;
begin
Result := 0;
{
FNV hashes are stored with little endianness, meaning first byte in memory
is the least significant. But when comparing, we must compare the most
significant bytes first, therefore going backwards.
}
For i := Pred(HashSize) downto 0 do
If AOverlay[i] <> BOverlay[i] then
begin
If AOverlay[i] > BOverlay[i] then
Result := +1
else
Result := -1;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
class Function TFNVBaseHash.HashAsString(const Hash): String;
var
HashOverlay: TFNVHashOverlay absolute Hash;
HashLength: Integer;
i: Integer;
begin
HashLength := HashSize;
Result := StringOfChar('0',HashLength * 2);
For i := 0 to Pred(HashSize) do
begin
Result[(i * 2) + 2] := IntToHex(HashOverlay[Pred(HashLength) - i] and $0F,1)[1];
Result[(i * 2) + 1] := IntToHex(HashOverlay[Pred(HashLength) - i] shr 4,1)[1];
end;
end;
//------------------------------------------------------------------------------
class procedure TFNVBaseHash.HashFromString(const Str: String; out Hash);
var
HashOverlay: TFNVHashOverlay absolute Hash;
HashLength: Integer;
WorkStr: String;
i: Integer;
begin
HashLength := Integer(HashSize);
If Length(Str) < (HashLength * 2) then
WorkStr := StringOfChar('0',(HashLength * 2) - Length(Str)) + Str
else If Length(Str) > (HashLength * 2) then
WorkStr := Copy(Str,Length(Str) - Pred(HashLength * 2),HashLength * 2)
else
WorkStr := Str;
For i := 0 to Pred(HashLength) do
HashOverlay[Pred(HashLength) - i] := UInt8(StrToInt('$' + Copy(WorkStr,(i * 2) + 1,2)));
end;
{-------------------------------------------------------------------------------
TFNVBaseHash - public methods
-------------------------------------------------------------------------------}
class Function TFNVBaseHash.HashEndianness: THashEndianness;
begin
Result := heLittle;
end;
//------------------------------------------------------------------------------
class Function TFNVBaseHash.HashFinalization: Boolean;
begin
Result := False;
end;
//------------------------------------------------------------------------------
Function TFNVBaseHash.HashName: String;
begin
case fHashAlgorithm of
algFNV0: Result := 'FNV-0';
algFNV1: Result := 'FNV-1';
algFNV1a: Result := 'FNV-1a';
else
raise EFNVInvalidValue.CreateFmt('TFNVBaseHash.HashName: Unknown hash algorithm (%d).',[Ord(fHashAlgorithm)]);
end;
end;
//------------------------------------------------------------------------------
constructor TFNVBaseHash.CreateAndInitFrom(Hash: THashBase);
begin
inherited CreateAndInitFrom(Hash);
If Hash is TFNVBaseHash then
ForceHashAlgorithm(TFNVBaseHash(Hash).HashAlgorithm)
else
raise EFNVIncompatibleClass.CreateFmt('TFNVBaseHash.CreateAndInitFrom: Incompatible class (%s).',[Hash.ClassName]);
end;
{===============================================================================
--------------------------------------------------------------------------------
TFNV32Hash
--------------------------------------------------------------------------------
===============================================================================}
const
FNV32Prime = TFNV32Sys($01000193);
{===============================================================================
TFNV32Hash - class declaration
===============================================================================}
{-------------------------------------------------------------------------------
TFNV32Hash - protected methods
-------------------------------------------------------------------------------}
Function TFNV32Hash.GetFNV32: TFNV32;
begin
Result := FNV32FromSys(fFNV32Value);
end;
//------------------------------------------------------------------------------
{$IFDEF OveflowChecks}{$Q-}{$ENDIF}
// overflows are disabled because of the multiplication
procedure TFNV32Hash.ProcessBuffer_FNV_0(const Buffer; Size: TMemSize);
var
Buff: PByte;
i: Integer;
begin
If Size > 0 then
begin
Buff := @Buffer;
For i := 0 to Pred(Size) do
begin
fFNV32Value := TFNV32Sys(fFNV32Value * FNV32Prime) xor TFNV32Sys(Buff^);
Inc(Buff);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TFNV32Hash.ProcessBuffer_FNV_1a(const Buffer; Size: TMemSize);
var
Buff: PByte;
i: Integer;
begin
If Size > 0 then
begin
Buff := @Buffer;
For i := 0 to Pred(Size) do
begin
fFNV32Value := TFNV32Sys((fFNV32Value xor TFNV32Sys(Buff^)) * FNV32Prime);
Inc(Buff);
end;
end;
end;
{$IFDEF OveflowChecks}{$Q+}{$ENDIF}
//------------------------------------------------------------------------------
procedure TFNV32Hash.Initialize;
begin
inherited;
fFNV32Value := FNV32ToSys(ZeroFNV32);
end;
{-------------------------------------------------------------------------------
TFNV32Hash - public methods
-------------------------------------------------------------------------------}
class Function TFNV32Hash.FNV32ToSys(Hash: TFNV32): TFNV32Sys;
begin
Result := TFNV32Sys(Hash);
{$IFDEF ENDIAN_BIG}HashSwapEndian(Result);{$ENDIF}
end;
//------------------------------------------------------------------------------
class Function TFNV32Hash.FNV32FromSys(Hash: TFNV32Sys): TFNV32;
begin
Result := TFNV32(Hash);
{$IFDEF ENDIAN_BIG}HashSwapEndian(Result);{$ENDIF}
end;
//------------------------------------------------------------------------------
class Function TFNV32Hash.FNV32ToLE(Hash: TFNV32): TFNV32;
begin
Result := Hash;
end;
//------------------------------------------------------------------------------
class Function TFNV32Hash.FNV32ToBE(Hash: TFNV32): TFNV32;
begin
Result := Hash;
HashSwapEndian(Result);
end;
//------------------------------------------------------------------------------
class Function TFNV32Hash.FNV32FromLE(Hash: TFNV32): TFNV32;
begin
Result := Hash;
end;
//------------------------------------------------------------------------------
class Function TFNV32Hash.FNV32FromBE(Hash: TFNV32): TFNV32;
begin
Result := Hash;
HashSwapEndian(Result);
end;
//------------------------------------------------------------------------------
class Function TFNV32Hash.HashSize: TMemSize;
begin
Result := SizeOf(TFNV32);
end;
//------------------------------------------------------------------------------
Function TFNV32Hash.HashName: String;
begin
Result := inherited HashName + '(32)';
end;
//------------------------------------------------------------------------------
constructor TFNV32Hash.CreateAndInitFrom(Hash: THashBase);
begin
inherited CreateAndInitFrom(Hash);
If Hash is TFNV32Hash then
fFNV32Value := TFNV32Hash(Hash).FNV32Sys
else
raise EFNVIncompatibleClass.CreateFmt('TFNV32Hash.CreateAndInitFrom: Incompatible class (%s).',[Hash.ClassName]);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TFNV32Hash.CreateAndInitFrom(Hash: TFNV32; HashAlgorithm: TFNVHashAlgorithm = algFNV1a);
begin
CreateAndInit;
ForceHashAlgorithm(HashAlgorithm);
fFNV32Value := FNV32ToSys(Hash);
end;
//------------------------------------------------------------------------------
procedure TFNV32Hash.Init;
begin
inherited;
If fHashAlgorithm = algFNV0 then
fFNV32Value := FNV32ToSys(ZeroFNV32)
else
fFNV32Value := FNV32ToSys(InitialFNV32);
end;
//------------------------------------------------------------------------------
Function TFNV32Hash.Compare(Hash: THashBase): Integer;
var
Local: TFNV32;
Remote: TFNV32;
begin
If Hash is TFNV32Hash then
begin
Local := FNV32FromSys(fFNV32Value);
Remote := TFNV32Hash(Hash).FNV32;
Result := HashCompare(Local,Remote);
end
else raise EFNVIncompatibleClass.CreateFmt('TFNV32Hash.Compare: Incompatible class (%s).',[Hash.ClassName]);
end;
//------------------------------------------------------------------------------
Function TFNV32Hash.AsString: String;
var
Temp: TFNV32;
begin
Temp := FNV32FromSys(fFNV32Value);
Result := HashAsString(Temp);
end;