-
Notifications
You must be signed in to change notification settings - Fork 1
/
Web.mORMot.CryptoUtils.pas
80 lines (60 loc) · 1.97 KB
/
Web.mORMot.CryptoUtils.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
unit Web.mORMot.CryptoUtils;
{-------------------------------------------------------------------------------
Adapted from mORMot v1 SynCrossPlatformCrypto.pas.
See original file for copyright and licence information at:
https://github.com/synopse/mORMot
-------------------------------------------------------------------------------}
interface
uses
Web.mORMot.Types;
/// compute the zlib/deflate crc32 hash value on a supplied ASCII-7 buffer
function crc32ascii(aCrc32: hash32; const buf: string): hash32;
function shr0(c: hash32): hash32;
var
/// table used by crc32() function
// - table content is created from code in initialization section below
crc32tab: array of hash32;
implementation
//------------------------------------------------------------------------------
function crc32(aCrc32: hash32; const buf: array of byte): hash32;
var i: integer;
begin
result := shr0(not aCRC32);
for i := 0 to length(buf)-1 do
result := crc32tab[(result xor buf[i]) and $ff] xor (result shr 8);
result := shr0(not result);
end;
//------------------------------------------------------------------------------
function crc32ascii(aCrc32: hash32; const buf: string): hash32;
var
i: integer;
begin
result := shr0(not aCRC32);
for i := 1 to length(buf) do
result := crc32tab[(result xor ord(buf[i])) and $ff] xor (result shr 8);
result := shr0(not result);
end;
//------------------------------------------------------------------------------
function shr0(c: hash32): hash32;
begin
result := c shr 0;
end;
//------------------------------------------------------------------------------
procedure InitCrc32Tab;
var
i,n,crc: hash32;
begin
for i := 0 to 255 do begin
crc := i;
for n := 1 to 8 do
if (crc and 1) <> 0 then
// $edb88320 from polynomial p=(0,1,2,4,5,7,8,10,11,12,16,22,23,26)
crc := shr0((crc shr 1) xor $edb88320)
else
crc := crc shr 1;
crc32tab[i] := crc;
end;
end;
initialization
InitCrc32Tab;
end.