-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsimple_sha256.py
More file actions
238 lines (216 loc) · 5.08 KB
/
Copy pathsimple_sha256.py
File metadata and controls
238 lines (216 loc) · 5.08 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
# Author Dario Clavijo 2022
# GPLv3
# Based on wikipedia article https://en.wikipedia.org/wiki/SHA-2
# This implementation only uses the ALU operations: AND OR NEG SUB XOR SHIFT
# We are using the propery that op 'mod 2^n' equals to '& (n-1)'
from struct import unpack, pack
def ADD32(a, b):
return (a + b) & 0xFFFFFFFF
def ROTR32(x, y):
return ((x >> y) | (x << (32 - y))) & 0xFFFFFFFF
def sha256(message):
"""Implementation according to the wikipedia"""
tmp = message
# Initialize hash values:
# (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
H = [
0x6A09E667,
0xBB67AE85,
0x3C6EF372,
0xA54FF53A,
0x510E527F,
0x9B05688C,
0x1F83D9AB,
0x5BE0CD19,
]
# Initialize array of round constants:
# (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
k = [
0x428A2F98,
0x71374491,
0xB5C0FBCF,
0xE9B5DBA5,
0x3956C25B,
0x59F111F1,
0x923F82A4,
0xAB1C5ED5,
0xD807AA98,
0x12835B01,
0x243185BE,
0x550C7DC3,
0x72BE5D74,
0x80DEB1FE,
0x9BDC06A7,
0xC19BF174,
0xE49B69C1,
0xEFBE4786,
0x0FC19DC6,
0x240CA1CC,
0x2DE92C6F,
0x4A7484AA,
0x5CB0A9DC,
0x76F988DA,
0x983E5152,
0xA831C66D,
0xB00327C8,
0xBF597FC7,
0xC6E00BF3,
0xD5A79147,
0x06CA6351,
0x14292967,
0x27B70A85,
0x2E1B2138,
0x4D2C6DFC,
0x53380D13,
0x650A7354,
0x766A0ABB,
0x81C2C92E,
0x92722C85,
0xA2BFE8A1,
0xA81A664B,
0xC24B8B70,
0xC76C51A3,
0xD192E819,
0xD6990624,
0xF40E3585,
0x106AA070,
0x19A4C116,
0x1E376C08,
0x2748774C,
0x34B0BCB5,
0x391C0CB3,
0x4ED8AA4A,
0x5B9CCA4F,
0x682E6FF3,
0x748F82EE,
0x78A5636F,
0x84C87814,
0x8CC70208,
0x90BEFFFA,
0xA4506CEB,
0xBEF9A3F7,
0xC67178F2,
]
# Pre-processing (Padding):
L = len(message)
K = 0
while (L + 1 + K + 8) & 63 != 0:
K += 1
tmp += b"\x80" + bytes(K)
tmp += pack("!Q", L << 3)
W = [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]
# Process the message in successive 512-bit chunks:
for chunk_index in range(0, len(tmp), 64):
W[:16] = unpack("!16L", tmp[chunk_index : chunk_index + 64])
# print(W)
# Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
for i in range(16, 64):
s0 = ROTR32(W[i - 15], 7) ^ ROTR32(W[i - 15], 18) ^ (W[i - 15] >> 3)
s1 = ROTR32(W[i - 2], 17) ^ ROTR32(W[i - 2], 19) ^ (W[i - 2] >> 10)
W[i] = (W[i - 16] + s0 + W[i - 7] + s1) & 0xFFFFFFFF
# Initialize working variables to current hash value:
a, b, c, d, e, f, g, h = H
# Compression function main loop:
for i in range(64):
S0 = ROTR32(a, 2) ^ ROTR32(a, 13) ^ ROTR32(a, 22)
S1 = ROTR32(e, 6) ^ ROTR32(e, 11) ^ ROTR32(e, 25)
ch = (e & f) ^ ((~e) & g)
maj = (a & b) ^ (a & c) ^ (b & c)
temp1 = (h + S1 + ch + k[i] + W[i]) & 0xFFFFFFFF
temp2 = (S0 + maj) & 0xFFFFFFFF
h = g
g = f
f = e
e = (d + temp1) & 0xFFFFFFFF
d = c
c = b
b = a
a = (temp1 + temp2) & 0xFFFFFFFF
# Add the compressed chunk to the current hash value:
H[0] = (H[0] + a) & 0xFFFFFFFF
H[1] = (H[1] + b) & 0xFFFFFFFF
H[2] = (H[2] + c) & 0xFFFFFFFF
H[3] = (H[3] + d) & 0xFFFFFFFF
H[4] = (H[4] + e) & 0xFFFFFFFF
H[5] = (H[5] + f) & 0xFFFFFFFF
H[6] = (H[6] + g) & 0xFFFFFFFF
H[7] = (H[7] + h) & 0xFFFFFFFF
# Produce the final hash value (big-endian):
return pack("!8L", *H)
def test():
assert (
sha256("".encode("utf8")).hex()
== "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)
assert (
sha256("The quick brown fox jumps over the lazy dog".encode("utf8")).hex()
== "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
)
if __name__ == "__main__":
test()