I am trying to implement a SHA-256 hashing function in python3 without importing any module, and when I run it with the input message 1, the output is 5d235a3ee467c78e6826419950a99c885245e09d0e53697afaa4c34fbca31b0e. However, when I check the hash value online for the same input, it's 6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b. I'm not sure why there's a discrepancy. Could someone please review my Python script and provide insights on what might be causing this difference?
def sha256(message):
# Pre-processing
message = bytearray(message, 'utf-8')
ml = len(message) * 8
message.append(0x80)
while len(message) % 64 != 56:
message.append(0x00)
message += ml.to_bytes(8, byteorder='big')
# Initialize hash values
h = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
]
# Constants
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
]
# Helper functions
def rotr(x, n):
return (x >> n) | (x << (32 - n)) & 0xFFFFFFFF
def shr(x, n):
return x >> n
def ch(x, y, z):
return (x & y) ^ (~x & z)
def maj(x, y, z):
return (x & y) ^ (x & z) ^ (y & z)
def sigma0(x):
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22)
def sigma1(x):
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25)
def gamma0(x):
return rotr(x, 7) ^ rotr(x, 18) ^ shr(x, 3)
def gamma1(x):
return rotr(x, 17) ^ rotr(x, 19) ^ shr(x, 10)
# Main loop
for i in range(0, len(message), 64):
w = [0] * 64
for t in range(16):
w[t] = int.from_bytes(message[i + t*4:i + t*4 + 4], byteorder='big')
for t in range(16, 64):
w[t] = (gamma1(w[t-2]) + w[t-7] + gamma0(w[t-15]) + w[t-16]) & 0xFFFFFFFF
a, b, c, d, e, f, g, h = h
for t in range(64):
t1 = h + sigma1(e) + ch(e, f, g) + k[t] + w[t]
t2 = sigma0(a) + maj(a, b, c)
h = g
g = f
f = e
e = (d + t1) & 0xFFFFFFFF
d = c
c = b
b = a
a = (t1 + t2) & 0xFFFFFFFF
h = [
(h + a) & 0xFFFFFFFF,
(g + b) & 0xFFFFFFFF,
(f + c) & 0xFFFFFFFF,
(e + d) & 0xFFFFFFFF,
(h + e) & 0xFFFFFFFF,
(f + f) & 0xFFFFFFFF,
(d + g) & 0xFFFFFFFF,
(c + h) & 0xFFFFFFFF
]
# Final hash value
return ''.join(format(x, '08x') for x in h)
message = '1'
hash_value = sha256(message)
print (hash_value)
Any help or suggestions would be greatly appreciated!
1 have carefully reviewed the SHA-256 implementation in my Python script, verified the input data, and cross-checked the algorithm with online SHA-256 calculators. I also experimented with different input messages to ensure consistency. Despite these efforts, the output from my script still differs from the expected hash value. I'm seeking guidance on potential areas to investigate or any errors in the provided code that might be causing this discrepancy.