127 lines
5.7 KiB
Python
127 lines
5.7 KiB
Python
class SHA256Custom:
|
|
"""
|
|
Custom SHA256 function, its not really custom but rewritten using the pseudocode from wikipedia and
|
|
should mimic the built in function
|
|
"""
|
|
def __init__(self, data=None):
|
|
# Setting up the initial bit values, i wont pretend to understand the math involved but these seem hardcoded
|
|
self.__ks = [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]
|
|
|
|
self.__hs = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
|
|
|
|
# Copy the initial bit values for use since they are changed
|
|
self.__h = self.__hs[:]
|
|
self.__k = self.__ks[:]
|
|
|
|
# Create variables we will need in the class
|
|
self.__string_byte_len = 0
|
|
self.__buffer = b''
|
|
self.__string_hashed = False
|
|
self.__digested_string = b''
|
|
|
|
if data is not None:
|
|
self.update(data)
|
|
|
|
# Static methods because pycharm is moaning about them not needing to be inside of the class
|
|
@staticmethod
|
|
def pad_string(string_len: int) -> bytes:
|
|
"""Static method to pad the string digest for the hash"""
|
|
# Adding a "1" bit to the end, although it seems to use 63 as the actual value, im guessing its possibly to do
|
|
# with chunk size
|
|
string_di = string_len & 0x3f
|
|
# Get the 64bit big endian length of the post processed hash length
|
|
post_process_length = (string_len << 3).to_bytes(8, "big")
|
|
padded_length = 55
|
|
if string_di < 56:
|
|
padded_length -= string_di
|
|
else:
|
|
padded_length -= 199 - string_di
|
|
|
|
return b'\x80' + b'\x00' * padded_length + post_process_length
|
|
|
|
@staticmethod
|
|
def rotate_right(a, b):
|
|
"""Static method to rotate a and b bits right """
|
|
return ((a >> b) | (a << (32 - b))) & 0xffffffff
|
|
|
|
def update(self, string_in: bytes) -> None:
|
|
"""Method to update the hash value"""
|
|
if (string_in and len(string_in) > 0) and not self.__string_hashed:
|
|
self.__string_byte_len += len(string_in)
|
|
string_buffer = self.__buffer + string_in
|
|
|
|
# Process the string as 512 bit chunks (64 bytes)
|
|
for i in range(0, len(string_buffer) // 64):
|
|
self.calculate_hash(string_buffer[64 * i:64 * (i + 1)])
|
|
|
|
# Set the class buffer
|
|
self.__buffer = string_buffer[len(string_buffer) - (len(string_buffer) % 64):]
|
|
else:
|
|
return
|
|
|
|
def digest(self) -> bytes:
|
|
"""Method to get the bytes digest of our hash value"""
|
|
if not self.__string_hashed:
|
|
self.update(self.pad_string(self.__string_byte_len))
|
|
self.__digested_string = b''.join(val.to_bytes(4, 'big') for val in self.__h[:8])
|
|
self.__string_hashed = True
|
|
return self.__digested_string
|
|
|
|
def hexdigest(self) -> str:
|
|
"""Method to get the hex digest of our hash value"""
|
|
# Hex characters to use when changing the hex value back into a string
|
|
tab = '0123456789abcdef'
|
|
return "".join(tab[byt >> 4] + tab[byt & 0xf] for byt in self.digest())
|
|
|
|
def calculate_hash(self, chunk) -> None:
|
|
"""Method to calculate the actual hash value"""
|
|
# Create an array of 32bit words
|
|
words = [0] * 64
|
|
|
|
# Converted from the pseudo code, but i believe its filling the word array with first 16 words we add
|
|
words[0:16] = [int.from_bytes(chunk[i:i + 4], "big") for i in range(0, len(chunk), 4)]
|
|
|
|
for i in range(16, 64):
|
|
s0 = self.rotate_right(words[i - 15], 7) ^ self.rotate_right(words[i - 15], 18) ^ (words[i - 15] >> 3)
|
|
s1 = self.rotate_right(words[i - 2], 17) ^ self.rotate_right(words[i - 2], 19) ^ (words[i - 2] >> 10)
|
|
words[i] = (words[i - 16] + s0 + words[i - 7] + s1) & 0xffffffff
|
|
|
|
# Initialize the values with the default hash value
|
|
a, b, c, d, e, f, g, h = self.__h
|
|
|
|
# Calculation main loop
|
|
for i in range(64):
|
|
s0 = self.rotate_right(a, 2) ^ self.rotate_right(a, 13) ^ self.rotate_right(a, 22)
|
|
t2 = s0 + self.maj(a, b, c)
|
|
s1 = self.rotate_right(e, 6) ^ self.rotate_right(e, 11) ^ self.rotate_right(e, 25)
|
|
t1 = h + s1 + self.ch(e, f, g) + self.__k[i] + words[i]
|
|
|
|
h = g
|
|
g = f
|
|
f = e
|
|
e = (d + t1) & 0xffffffff
|
|
d = c
|
|
c = b
|
|
b = a
|
|
a = (t1 + t2) & 0xffffffff
|
|
|
|
for i, (x, y) in enumerate(zip(self.__h, [a, b, c, d, e, f, g, h])):
|
|
self.__h[i] = (x + y) & 0xffffffff
|
|
|
|
@staticmethod
|
|
def maj(a, b, c):
|
|
"""Static method for the calculation of the maj variable in the pseudo code"""
|
|
return (a & b) ^ (a & c) ^ (b & c)
|
|
|
|
@staticmethod
|
|
def ch(a, b, c):
|
|
"""Static method to calculate the ch variable in the pseudo code"""
|
|
return (a & b) ^ ((~a) & c)
|