-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathClpDigest.pas
More file actions
215 lines (171 loc) · 5.18 KB
/
Copy pathClpDigest.pas
File metadata and controls
215 lines (171 loc) · 5.18 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
{ * CryptoLib Library * }
{ * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
{ * Github Repository <https://github.com/Xor-el> * }
{ * Distributed under the MIT software license, see the accompanying file LICENSE * }
{ * or visit http://www.opensource.org/licenses/mit-license.php. * }
{ * Acknowledgements: * }
{ * * }
{ * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
{ * development of this library * }
{ * ******************************************************************************* * }
(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
unit ClpDigest;
{$I ..\..\Include\CryptoLib.inc}
interface
uses
SysUtils,
HlpIHash,
HlpIHashInfo,
ClpIDigest,
ClpCryptoLibTypes;
resourcestring
SOutputBufferTooShort = 'Output Buffer Too Short';
type
/// <summary>
/// Hash Wrapper For the Proper Implementation in HashLib4Pascal
/// </summary>
TDigest = class sealed(TInterfacedObject, IDigest)
strict private
var
FHash: IHash;
function GetAlgorithmName: string; inline;
function DoFinal: TCryptoLibByteArray; overload;
public
constructor Create(const hash: IHash; doInitialize: Boolean = True);
/// <summary>
/// Gets the Underlying <b>IHash</b> Instance
/// </summary>
function GetUnderlyingIHash: IHash; inline;
/// <summary>
/// the size, in bytes, of the digest produced by this message digest.
/// </summary>
function GetDigestSize(): Int32; inline;
/// <summary>
/// the size, in bytes, of the internal buffer used by this digest.
/// </summary>
function GetByteLength(): Int32; inline;
/// <summary>
/// update the message digest with a single byte.
/// </summary>
procedure Update(input: Byte);
/// <summary>
/// update the message digest with a block of bytes.
/// </summary>
/// <param name="input">
/// the byte array containing the data.
/// </param>
/// <param name="inOff">
/// the offset into the byte array where the data starts.
/// </param>
/// <param name="len">
/// the length of the data.
/// </param>
procedure BlockUpdate(const input: TCryptoLibByteArray; inOff, len: Int32);
/// <summary>
/// Close the digest, producing the final digest value. The doFinal call
/// leaves the digest reset.
/// </summary>
/// <param name="output">
/// the array the digest is to be copied into.
/// </param>
/// <param name="outOff">
/// the offset into the out array the digest is to start at.
/// </param>
function DoFinal(const output: TCryptoLibByteArray; outOff: Int32)
: Int32; overload;
/// <summary>
/// Resets the digest back to it's initial state.
/// </summary>
procedure Reset();
/// <summary>
/// Clone the digest instance
/// </summary>
function Clone(): IDigest;
/// <summary>
/// the algorithm name
/// </summary>
property AlgorithmName: String read GetAlgorithmName;
end;
implementation
{ TDigest }
function TDigest.GetAlgorithmName: string;
var
LName: String;
LowPoint, HighPoint: Int32;
begin
LName := FHash.Name;
LowPoint := 1;
HighPoint := System.Length(LName);
result := Copy(LName, LowPoint + 1, HighPoint - 1);
end;
function TDigest.GetByteLength: Int32;
begin
result := FHash.BlockSize;
end;
function TDigest.GetDigestSize: Int32;
begin
result := FHash.HashSize;
end;
function TDigest.GetUnderlyingIHash: IHash;
begin
result := FHash;
end;
procedure TDigest.Reset;
begin
FHash.Initialize;
end;
procedure TDigest.BlockUpdate(const input: TCryptoLibByteArray;
inOff, len: Int32);
begin
FHash.TransformBytes(input, inOff, len);
end;
constructor TDigest.Create(const hash: IHash; doInitialize: Boolean);
begin
Inherited Create();
FHash := hash;
if doInitialize then
begin
FHash.Initialize;
end;
end;
function TDigest.DoFinal(const output: TCryptoLibByteArray;
outOff: Int32): Int32;
var
buf: TCryptoLibByteArray;
Limit, LXOFSizeInBits: Int32;
begin
if Supports(FHash, IXOF) then
begin
LXOFSizeInBits := (System.Length(output) - outOff) * 8;
(FHash as IXOF).XOFSizeInBits := LXOFSizeInBits;
Limit := LXOFSizeInBits shr 3;
end
else
begin
Limit := GetDigestSize;
end;
if (System.Length(output) - outOff) < Limit then
begin
raise EDataLengthCryptoLibException.CreateRes(@SOutputBufferTooShort);
end
else
begin
buf := DoFinal();
System.Move(buf[0], output[outOff], System.Length(buf) *
System.SizeOf(Byte));
end;
result := System.Length(buf);
end;
function TDigest.DoFinal: TCryptoLibByteArray;
begin
result := FHash.TransformFinal.GetBytes();
end;
procedure TDigest.Update(input: Byte);
begin
FHash.TransformUntyped(input, System.SizeOf(Byte));
end;
function TDigest.Clone(): IDigest;
begin
result := TDigest.Create(FHash.Clone(), False);
end;
end.