-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sha1.cpp
182 lines (146 loc) · 3.13 KB
/
Sha1.cpp
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
#include "Sha1.h"
#include <string>
#include <iostream>
#include <stdint.h>
using namespace std;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef uint8_t uint8;
// n must be 0 <= n < 32
uint32 S(uint32 x, int n)
{
// n %= 32;
return (x << n) | (x >> (32-n));
}
// Round functions
uint32 f1(uint32 b, uint32 c, uint32 d)
{
return (b & c) | ((~b) & d);
}
uint32 f24(uint32 b, uint32 c, uint32 d)
{
return b ^ c ^ d;
}
uint32 f3(uint32 b, uint32 c, uint32 d)
{
return (b & c) | (b & d) | (c & d);
}
uint32 f(uint32 b, uint32 c, uint32 d, int t)
{
if (t < 20)
return f1(b, c, d);
if (t < 40)
return f24(b, c, d);
if (t < 60)
return f3(b, c, d);
return f24(b, c, d);
}
// Round constants.
const uint32 K1 = 0x5A827999;
const uint32 K2 = 0x6ED9EBA1;
const uint32 K3 = 0x8F1BBCDC;
const uint32 K4 = 0xCA62C1D6;
uint32 K(int t)
{
if (t < 20)
return K1;
if (t < 40)
return K2;
if (t < 60)
return K3;
return K4;
}
char IntToHex(int C)
{
switch (C)
{
case 0: return '0';
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return '4';
case 5: return '5';
case 6: return '6';
case 7: return '7';
case 8: return '8';
case 9: return '9';
case 10: return 'a';
case 11: return 'b';
case 12: return 'c';
case 13: return 'd';
case 14: return 'e';
case 15: return 'f';
}
return '-';
}
string Hash(string msg)
{
// We will just hash one 512 bit block.
// For simplicity we will therefore limit the
// msg to 512-64-1 = 440 bits (rounded down to 55 bytes.
if (msg.length() > 55)
msg.resize(55);
uint8 block[64];
for (int i = 0; i < 64; ++i)
block[i] = 0;
for (unsigned int i = 0; i < msg.length(); ++i)
block[i] = msg[i];
// Append a '1' bit (we're in big endian bit order)
block[msg.length()] = 0x80;
// Set the last 64 bits to the length of the message in bits in the original message.
int bits = msg.length() * 8;
// Big endian, and it can't be more than 55*8=440.
block[62] = (bits >> 8) & 0xFF;
block[63] = (bits) & 0xFF;
// Now the main computation.
uint32 A, B, C, D, E;
uint32 H[5];
H[0] = 0x67452301;
H[1] = 0xEFCDAB89;
H[2] = 0x98BADCFE;
H[3] = 0x10325476;
H[4] = 0xC3D2E1F0;
uint32 W[80];
for (int i = 0; i < 80; ++i) // Probably not needed.
W[i] = 0;
uint32 temp;
// Fill W.
for (int i = 0; i < 16; ++i)
W[i] = (block[i*4] << 24) | (block[i*4 + 1] << 16) | (block[i*4 + 2] << 8) | (block[i*4 + 3]);
// Do computation.
for (int t = 16; t < 80; ++t)
W[t] = S(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
A = H[0];
B = H[1];
C = H[2];
D = H[3];
E = H[4];
for (int t = 0; t < 80; ++t)
{
temp = S(A, 5) + f(B, C, D, t) + E + W[t] + K(t);
E = D;
D = C;
C = S(B, 30);
B = A;
A = temp;
}
H[0] += A;
H[1] += B;
H[2] += C;
H[3] += D;
H[4] += E;
// The hash is now H0-4.
string ret;
for (int i = 0; i < 5; ++i)
{
ret += IntToHex((H[i] >> 28) & 0xF);
ret += IntToHex((H[i] >> 24) & 0xF);
ret += IntToHex((H[i] >> 20) & 0xF);
ret += IntToHex((H[i] >> 16) & 0xF);
ret += IntToHex((H[i] >> 12) & 0xF);
ret += IntToHex((H[i] >> 8) & 0xF);
ret += IntToHex((H[i] >> 4) & 0xF);
ret += IntToHex((H[i]) & 0xF);
}
return ret;
}