Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def byte_length(n: int):
- return (n.bit_length() + 7) // 8
- def leftrotate(n: int, count: int):
- """leftrotate count times. count must be between 1 and 31 inclusive"""
- return (n << count) | (n >> (32 - count)) & 0xFFFFFFFF
- def add32(ns: list[int]):
- """add integers as if they were unsigned 32 bit numbers"""
- sum = 0
- for n in ns:
- sum = (sum + n) & 0xFFFFFFFF
- return sum
- def append_bytes(m: bytearray):
- # append 0 ≤ k < 512 bits '0', such that the resulting message length in bits
- # is congruent to −64 ≡ 448 (mod 512)
- modular_length = len(m) % 64
- if 56 - modular_length >= 0:
- k = 56 - modular_length
- else:
- k = abs(56 - modular_length) + 56
- m += bytes(k)
- return m
- def preprocessing(m: bytearray) -> bytearray:
- ml = len(m) * 8 # message length in bits
- # Pre Processing
- # append the bit '1' to the message e.g. by adding 0x80 if message length is a multiple of 8 bits.
- m.append(0x80)
- m = append_bytes(m)
- # append the message length as a 64 bit integer
- m += ml.to_bytes(8)
- return m
- def sha1(message: str, iterations: int) -> bytes:
- m = bytearray(message, "utf-8")
- # Initiliaze starting variables, so called "nothing up my sleeve values"
- h0 = 0x67452301
- h1 = 0xEFCDAB89
- h2 = 0x98BADCFE
- h3 = 0x10325476
- h4 = 0xC3D2E1F0
- m = preprocessing(m)
- # split the message in 512 bit (64 byte) blocks
- num_of_blocks: int = len(m) // 64
- for block_num in range(num_of_blocks):
- block: bytearray = m[block_num * 64:(block_num + 1) * 64]
- # split the block into sixteen 4 byte words
- words: list[int] = []
- for word_num in range(16):
- words.append(int.from_bytes(
- block[word_num * 4:(word_num + 1) * 4]))
- # extend the sixteen 4 byte words into eighty 4 byte words
- for i in range(16, iterations):
- words.append(
- leftrotate(
- (words[i - 3] ^ words[i - 8] ^
- words[i - 14] ^ words[i - 16]), 1
- )
- )
- # Initiliaze the hash value of the current block
- a = h0
- b = h1
- c = h2
- d = h3
- e = h4
- # main loop
- for i in range(iterations):
- if i <= 19:
- f = ((b & c) | ((~b) & d)) & 0xFFFFFFFF
- k = 0x5A827999
- elif i <= 39:
- f = (b ^ c ^ d) & 0xFFFFFFFF
- k = 0x6ED9EBA1
- elif i <= 59:
- f = ((b & c) | (b & d) | (c & d)) & 0xFFFFFFFF
- k = 0x8F1BBCDC
- # elif i <= 79:
- else:
- f = (b ^ c ^ d) & 0xFFFFFFFF
- k = 0xCA62C1D6
- temp = add32([leftrotate(a, 5), f, e, k, words[i]]) & 0xFFFFFFFF
- e = d & 0xFFFFFFFF
- d = c & 0xFFFFFFFF
- c = leftrotate(b, 30) & 0xFFFFFFFF
- b = a & 0xFFFFFFFF
- a = temp & 0xFFFFFFFF
- h0 = add32([h0, a])
- h1 = add32([h1, b])
- h2 = add32([h2, c])
- h3 = add32([h3, d])
- h4 = add32([h4, e])
- digest = (
- h0.to_bytes(4)
- + h1.to_bytes(4)
- + h2.to_bytes(4)
- + h3.to_bytes(4)
- + h4.to_bytes(4)
- )
- return digest
- def main():
- m = "hello world"
- for i in range(16, 81):
- digest = sha1(m, i)
- print(f"iteration {i}: {hex(int.from_bytes(digest))}")
- main()
Advertisement
Add Comment
Please, Sign In to add comment