-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHA-256.py
163 lines (148 loc) · 8.25 KB
/
SHA-256.py
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
#Full version of SHA-256, takes a string of any length and computes the hash
#main function, takes string
def generate_hash(input):
K = [[0,1,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1,1,1,1,0,0,1,1,0,0,0],[0,1,1,1,0,0,0,1,0,0,1,1,0,1,1,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1],
[1,0,1,1,0,1,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1],[1,1,1,0,1,0,0,1,1,0,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0,1,0,0,1,0,1],
[0,0,1,1,1,0,0,1,0,1,0,1,0,1,1,0,1,1,0,0,0,0,1,0,0,1,0,1,1,0,1,1],[0,1,0,1,1,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,1],
[1,0,0,1,0,0,1,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,0,1,0,1,0,0,1,0,0],[1,0,1,0,1,0,1,1,0,0,0,1,1,1,0,0,0,1,0,1,1,1,1,0,1,1,0,1,0,1,0,1],
[1,1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,0,1,0,1,0,1,0,0,1,1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1,1]
h7 = [0,1,0,1,1,0,1,1,1,1,1,0,0,0,0,0,1,1,0,0,1,1,0,1,0,0,0,1,1,0,0,1]
message=[]
if isinstance(input, str):
for i in range (len(input)):
hold=ord(input[i])
for x in range (7,-1,-1):
if hold>=2**x:
message.append(1)
hold-=2**x
else:
message.append(0)
message.append(1)
while (len(message)+64)%512!=0:
message.append(0)
hold=len(input)*8
for x in range (63,-1,-1):
if hold>=2**x:
message.append(1)
hold-=2**x
else:
message.append(0)
else:
return 'ERROR: INPUT NOT STRING'
for p in range (len(message)//512):
chunk=message[p*512:(p+1)*512]
W = []
for i in range(0, 64):
if i <= 15:
W.append(chunk[i*32:(i*32)+32])
else:
s0=xor(xor(rightRotate(W[i-15],7),rightRotate(W[i-15],18)),rightShift(W[i-15],3))
s1=xor(xor(rightRotate(W[i-2],17),rightRotate(W[i-2],19)),rightShift(W[i-2],10))
W.append(plus(plus(plus(W[i-16],s0),W[i-7]),s1))
a = h0
b = h1
c = h2
d = h3
e = h4
f = h5
g = h6
h = h7
for i in range(64):
S1=xor(xor(rightRotate(e,6),rightRotate(e,11)),rightRotate(e,25))
ch=xor(ander(e,f),ander(noter(e),g))
temp1=plus(plus(plus(plus(h,S1),ch),K[i]),W[i])
S0=xor(xor(rightRotate(a,2),rightRotate(a,13)),rightRotate(a,22))
maj=xor(xor(ander(a,b),ander(a,c)),ander(b,c))
temp2=plus(S0,maj)
h = g
g = f
f = e
e = plus(d,temp1)
d = c
c = b
b = a
a = plus(temp1,temp2)
h0 = plus(h0,a)
h1 = plus(h1,b)
h2 = plus(h2,c)
h3 = plus(h3,d)
h4 = plus(h4,e)
h5 = plus(h5,f)
h6 = plus(h6,g)
h7 = plus(h7,h)
return h0+h1+h2+h3+h4+h5+h6+h7
#basic rotation
def rightRotate(arr,dist):
ret=[]
for x in range (32):
ret.append(arr[(x-dist)%32])
return ret
#shift over, leave 0's in wake
def rightShift(arr,dist):
ret=[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]
for x in range (dist,32):
ret[x]=arr[x-dist]
return ret
#invert every value
def noter(arr):
ret=[]
for x in range (32):
ret.append(1-arr[x])
return ret
#apply xor to every pair of bits
def xor(arr1,arr2):
ret=[]
for x in range (32):
ret.append(int(arr1[x]!=arr2[x]))
return ret
#apply and to every pair of bits
def ander(arr1,arr2):
ret=[]
for x in range (32):
ret.append(1 if arr1[x] and arr2[x] else 0)
return ret
#binary addition, discard overflow
def plus(arr1,arr2):
ret=[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]
carry=0
for x in range(31, -1, -1):
r = carry+arr1[x]+arr2[x]
ret[x] = int(r%2==1)
carry = int(r>=2)
return ret
if __name__ == "__main__":
print(generate_hash('hello world'))