-
Notifications
You must be signed in to change notification settings - Fork 32
/
sha1.d
252 lines (208 loc) · 5.9 KB
/
sha1.d
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
import std.stdio;
import std.conv;
enum
{
shaSuccess = 0,
shaNull, /* Null pointer parameter */
shaInputTooLong, /* input data too long */
shaStateError /* called Input after Result */
}
immutable uint SHA1HashSize = 20;
/*
* This structure will hold context information for the SHA-1
* hashing operation
*/
struct SHA1
{
private:
uint[SHA1HashSize/4] Intermediate_Hash; /* Message Digest */
uint Length_Low; // Message length in bits
uint Length_High; // Message length in bits
// Index into message block array
short Message_Block_Index;
ubyte[64] Message_Block; /* 512-bit message blocks */
bool Computed; /* Is the digest computed? */
bool Corrupted; /* Is the message digest corrupted? */
int err;
uint SHA1CircularShift(int bits, uint word)
{
return (((word) << (bits)) | ((word) >> (32-(bits))));
}
void ProcessMessageBlock()
{
const uint K[] = [ 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 ];
int t; /* Loop counter */
uint temp; /* Temporary word value */
uint[80] W; /* Word sequence */
uint A, B, C, D, E; /* Word buffers */
/*
* Initialize the first 16 words in the array W
*/
for(t = 0; t < 16; t++)
{
W[t] = Message_Block[t * 4] << 24;
W[t] |= Message_Block[t * 4 + 1] << 16;
W[t] |= Message_Block[t * 4 + 2] << 8;
W[t] |= Message_Block[t * 4 + 3];
}
for(t = 16; t < 80; t++)
{
W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
}
A = Intermediate_Hash[0];
B = Intermediate_Hash[1];
C = Intermediate_Hash[2];
D = Intermediate_Hash[3];
E = Intermediate_Hash[4];
for(t = 0; t < 20; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | ((~B) & D)) + E + W[t] + K[0];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 20; t < 40; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 40; t < 60; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
for(t = 60; t < 80; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
E = D;
D = C;
C = SHA1CircularShift(30,B);
B = A;
A = temp;
}
Intermediate_Hash[0] += A;
Intermediate_Hash[1] += B;
Intermediate_Hash[2] += C;
Intermediate_Hash[3] += D;
Intermediate_Hash[4] += E;
Message_Block_Index = 0;
}
void PadMessage()
{
if (Message_Block_Index > 55)
{
Message_Block[Message_Block_Index++] = 0x80;
while(Message_Block_Index < 64)
{
Message_Block[Message_Block_Index++] = 0;
}
ProcessMessageBlock();
while(Message_Block_Index < 56)
{
Message_Block[Message_Block_Index++] = 0;
}
}
else
{
Message_Block[Message_Block_Index++] = 0x80;
while(Message_Block_Index < 56)
{
Message_Block[Message_Block_Index++] = 0;
}
}
/*
* Store the message length as the last 8 octets
*/
Message_Block[56] = cast(ubyte) (Length_High >> 24);
Message_Block[57] = cast(ubyte) (Length_High >> 16);
Message_Block[58] = cast(ubyte) (Length_High >> 8);
Message_Block[59] = cast(ubyte) Length_High;
Message_Block[60] = cast(ubyte) (Length_Low >> 24);
Message_Block[61] = cast(ubyte) (Length_Low >> 16);
Message_Block[62] = cast(ubyte) (Length_Low >> 8);
Message_Block[63] = cast(ubyte) Length_Low;
ProcessMessageBlock();
}
public:
this(int dummy = 0) { reset(); dummy++; }
@property error() { return err; }
void reset()
{
Length_Low = 0;
Length_High = 0;
Message_Block_Index = 0;
Intermediate_Hash = [ 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 ];
Computed = false;
Corrupted = false;
}
ubyte[] result()
{
if (Corrupted)
return null;
ubyte[] rv;
rv.length = SHA1HashSize;
if (!Computed)
{
PadMessage();
Message_Block[] = 0;
Length_Low = 0; /* and clear length */
Length_High = 0;
Computed = true;
}
for (int i = 0; i < SHA1HashSize; i++)
{
rv[i] = cast(ubyte) (Intermediate_Hash[i>>2] >> 8 * ( 3 - ( i & 0x03 )));
}
return rv;
}
bool input(const(ubyte)* message_array, uint length)
{
if (!length)
return true;
if (message_array is null)
{
err = shaNull;
return false;
}
if (Computed)
{
err = shaStateError;
Corrupted = true;
return false;
}
if (Corrupted)
return false;
while(length-- && !Corrupted)
{
Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
Length_Low += 8;
if (Length_Low == 0) // wrapped round
{
Length_High++;
if (Length_High == 0) // wrapped round
{
// Message is too long
Corrupted = true;
err = shaInputTooLong;
return false;
}
}
if (Message_Block_Index == 64)
ProcessMessageBlock();
message_array++;
}
return true;
}
}