'Why is my hash value off did I do something wrong with my code?
I am writing a SHA256 Hash function and it's mostly complete but when I run the code the hash is way to high and it's not correct. What is wrong with my code that's making it far off from the actual value to be? Now keep in mind I do not have a chunk loop so it might look a bit different. This also A GUI as well.
#VARIABLES
FF = (1 >> 32)-1
#Right rotate
def rr(a,b):
return((a >> b) | (a << (32 - b))) & FF
h0 = 0x6a09e667
h1 = 0xbb67ae85
h2 = 0x3c6ef372
h3 = 0xa54ff53a
h4 = 0x510e527f
h5 = 0x9b05688c
h6 = 0x1f83d9ab
h7 = 0x5be0cd19
#Messages
word_Hash = "1: Please write below what\n word you would like to hash."
words = []
playerinput = []
varwords = [word_Hash]
#FUNCTIONS
count = 0
def SHA256():
global playerinput, wordtohash, A, B, C, D, E, F, G, H
#get input
wordtohash = playerinput.pop(0)
#convert to binary
BinaryConversion = ''.join(format(ord(i), '08b') for i in wordtohash) + "1"
#pad helped by Dr.Glynn, Maple
if len(BinaryConversion) <= 512:
count = len(BinaryConversion)
while count <= 448:
BinaryConversion = BinaryConversion + "0"
count += 1
wordtohash = len(wordtohash)
endofpad = int(wordtohash) * 8
numberofzeros = 0
while numberofzeros < 63 - int(len(bin(endofpad)[2:])):
BinaryConversion = BinaryConversion + "0"
numberofzeros += 1
#BinaryConversion = (BinaryConversion) + str(endofpad)
BinaryConversion = str(BinaryConversion) + str(bin(endofpad)[2:])
#numbers = len(BinaryConversion)
#print(BinaryConversion)
#first 16 messages
w = [int('0b'+BinaryConversion[0:31], 2),
int('0b'+BinaryConversion[32:63], 2),
int('0b'+BinaryConversion[64:95],2),
int('0b'+BinaryConversion[96:127],2),
int('0b'+BinaryConversion[128:159],2),
int('0b'+BinaryConversion[160:191],2),
int('0b'+BinaryConversion[192:223],2),
int('0b'+BinaryConversion[224:255],2),
int('0b'+BinaryConversion[256:287],2),
int('0b'+BinaryConversion[288:319],2),
int('0b'+BinaryConversion[320:351],2),
int('0b'+BinaryConversion[352:383],2),
int('0b'+BinaryConversion[384:415],2),
int('0b'+BinaryConversion[416:447],2),
int('0b'+BinaryConversion[448:479],2),
int('0b'+BinaryConversion[480:511],2)]
#Message Scedule
#rest of the messages
for c in range(16,64):
S0 = rr(w[c-15], 7) ^ rr(w[c-15], 18) ^ (w[c-15] >> 3)
S1 = rr(w[c - 2], 17) ^ rr(w[c - 2], 19) ^ (w[c - 2] >> 10)
w.append((w[c - 16] + S0 + w[c-7] + S1) & FF)
print(w)
A = h0
B = h1
C = h2
D = h3
E = h4
F = h5
G = h6
H = h7
for i in range(64):
s1 = rr(E, 6) ^ rr(E, 11) ^ rr(E, 25)
ch = (E & F) ^ (~E & G)
temp1 = H + s1 + ch + K[i] + w[i]
s0 = rr(A, 2) ^ rr(A, 13) ^ rr(A, 22)
maj = (A & B) ^ (A & C) ^ (B & C)
temp2 = s0 + maj
H = G
G = F
F = E
E = D + temp1
D = C
C = B
B = A
A = temp1 + temp2
A = hex(A)[2:]
B = hex(B)[2:]
C = hex(C)[2:]
D = hex(D)[2:]
E = hex(E)[2:]
F = hex(F)[2:]
G = hex(G)[2:]
H = hex(H)[2:]
print(A)
def finish():
global count, varwords, playerinput, words
if count >= 1:
screenframe1.pack_forget()
frm_screen2.pack()
SHA256()
lbl_story["text"] = "Your word is {}\n Your hash value is {}{}{}{}{}{}{}{}".format(wordtohash,A,B,C,D,E,F,G,H)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
