Debian Codegraph @

components/kascrypt/Hashes/dcpblake3.pas

raw
1 {******************************************************************************}
2 {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
3 {******************************************************************************}
4 {* A binary compatible implementation of BLAKE3 *}
5 {******************************************************************************}
6 {* Copyright (C) 2020 Alexander Koblov ([email protected]) *}
7 {* Permission is hereby granted, free of charge, to any person obtaining a *}
8 {* copy of this software and associated documentation files (the "Software"), *}
9 {* to deal in the Software without restriction, including without limitation *}
10 {* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
11 {* and/or sell copies of the Software, and to permit persons to whom the *}
12 {* Software is furnished to do so, subject to the following conditions: *}
13 {* *}
14 {* The above copyright notice and this permission notice shall be included in *}
15 {* all copies or substantial portions of the Software. *}
16 {* *}
17 {* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
18 {* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
19 {* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
20 {* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
21 {* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
22 {* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
23 {* DEALINGS IN THE SOFTWARE. *}
24 {******************************************************************************}
25 unit DCPblake3;
26
27 {$mode delphi}
28
29 interface
30
31 uses
32 Classes, SysUtils, CTypes, DCPcrypt2, DCPconst, DCblake3;
33
34 type
35
36 { TDCP_blake3 }
37
38 TDCP_blake3 = class(TDCP_hash)
39 protected
40 S: blake3_hasher;
41 public
42 class function GetId: integer; override;
43 class function GetAlgorithm: string; override;
44 class function GetHashSize: integer; override;
45 class function SelfTest: boolean; override;
46 procedure Init; override;
47 procedure Burn; override;
48 procedure Update(const Buffer; Size: longword); override;
49 procedure Final(var Digest); override;
50 end;
51
52 implementation
53
54 { TDCP_blake3 }
55
56 class function TDCP_blake3.GetId: integer;
57 begin
58 Result:= DCP_blake3;
59 end;
60
61 class function TDCP_blake3.GetAlgorithm: string;
62 begin
63 Result:= 'BLAKE3';
64 end;
65
66 class function TDCP_blake3.GetHashSize: integer;
67 begin
68 Result:= 256;
69 end;
70
71 class function TDCP_blake3.SelfTest: boolean;
72 const
73 Test1Out: array[0..31] of byte=
74 ($64, $37, $b3, $ac, $38, $46, $51, $33, $ff, $b6, $3b, $75, $27, $3a, $8d, $b5,
75 $48, $c5, $58, $46, $5d, $79, $db, $03, $fd, $35, $9c, $6c, $d5, $bd, $9d, $85);
76 Test2Out: array[0..31] of byte=
77 ($c1, $90, $12, $cc, $2a, $af, $0d, $c3, $d8, $e5, $c4, $5a, $1b, $79, $11, $4d,
78 $2d, $f4, $2a, $bb, $2a, $41, $0b, $f5, $4b, $e0, $9e, $89, $1a, $f0, $6f, $f8 );
79 var
80 TestHash: TDCP_blake3;
81 TestOut: array[0..31] of byte;
82 begin
83 dcpFillChar(TestOut, SizeOf(TestOut), 0);
84 TestHash:= TDCP_blake3.Create(nil);
85 TestHash.Init;
86 TestHash.UpdateStr('abc');
87 TestHash.Final(TestOut);
88 Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
89 TestHash.Init;
90 TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
91 TestHash.Final(TestOut);
92 Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
93 TestHash.Free;
94 end;
95
96 procedure TDCP_blake3.Init;
97 begin
98 blake3_hasher_init(@S);
99 fInitialized:= true;
100 end;
101
102 procedure TDCP_blake3.Burn;
103 begin
104 fInitialized:= false;
105 end;
106
107 procedure TDCP_blake3.Update(const Buffer; Size: longword);
108 begin
109 blake3_hasher_update(@S, @Buffer, Size);
110 end;
111
112 procedure TDCP_blake3.Final(var Digest);
113 var
114 Hash: array[0..Pred(BLAKE3_OUT_LEN)] of cuint8;
115 begin
116 if not fInitialized then
117 raise EDCP_hash.Create('Hash not initialized');
118 blake3_hasher_finalize(@S, Hash, SizeOf(Hash));
119 Move(Hash, Digest, Sizeof(Hash));
120 Burn;
121 end;
122
123 end.
124