Guest User

Untitled

a guest
Aug 21st, 2026
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. def byte_length(n: int):
  2. return (n.bit_length() + 7) // 8
  3.  
  4.  
  5. def leftrotate(n: int, count: int):
  6. """leftrotate count times. count must be between 1 and 31 inclusive"""
  7. return (n << count | n >> (32 - count)) & 0xFFFFFFFF
  8.  
  9.  
  10. def add32(ns: list[int]):
  11. """add integers as if they were unsigned 32 bit numbers"""
  12. sum = 0
  13. for n in ns:
  14. sum = (sum + n) & 0xFFFFFFFF
  15.  
  16. return sum
  17.  
  18.  
  19. def append_bytes(m: bytearray):
  20. # append 0 ≤ k < 512 bits '0', such that the resulting message length in bits
  21. # is congruent to −64 ≡ 448 (mod 512)
  22. modular_length = len(m) % 64
  23. if 56 - modular_length >= 0:
  24. k = 56 - modular_length
  25. else:
  26. k = abs(56 - modular_length) + 56
  27.  
  28. m += bytes(k)
  29. return m
  30.  
  31.  
  32. def preprocessing(m: bytearray) -> bytearray:
  33. ml = len(m) * 8 # message length in bits
  34.  
  35. # Pre Processing
  36. # append the bit '1' to the message e.g. by adding 0x80 if message length is a multiple of 8 bits.
  37. m.append(0x80)
  38.  
  39. m = append_bytes(m)
  40.  
  41. # append the message length as a 64 bit integer
  42. m += ml.to_bytes(8)
  43.  
  44. return m
  45.  
  46.  
  47. def sha1(message: str) -> bytes:
  48.  
  49. m = bytearray(message, "utf-8")
  50.  
  51. # Initiliaze starting variables, so called "nothing up my sleeve values"
  52. h0 = 0x67452301
  53. h1 = 0xEFCDAB89
  54. h2 = 0x98BADCFE
  55. h3 = 0x10325476
  56. h4 = 0xC3D2E1F0
  57.  
  58. m = preprocessing(m)
  59.  
  60. # split the message in 512 bit (64 byte) blocks
  61. num_of_blocks: int = len(m) // 64
  62. for block_num in range(num_of_blocks):
  63. block: bytearray = m[block_num * 64:(block_num + 1) * 64]
  64.  
  65. # split the block into sixteen 4 byte words
  66. words: list[int] = []
  67. for word_num in range(16):
  68. words.append(int.from_bytes(
  69. block[word_num * 4:(word_num + 1) * 4]))
  70.  
  71. # extend the sixteen 4 byte words into eighty 4 byte words
  72. for i in range(16, 80):
  73. words.append(
  74. leftrotate(
  75. (words[i - 3] ^ words[i - 8] ^
  76. words[i - 14] ^ words[i - 16]), 1
  77. )
  78. )
  79.  
  80. # Initiliaze the hash value of the current block
  81. a = h0
  82. b = h1
  83. c = h2
  84. d = h3
  85. e = h4
  86.  
  87. # main loop
  88. for i in range(80):
  89. if i <= 19:
  90. f = ((b & c) | ((~b) & d))
  91. k = 0x5A827999
  92.  
  93. elif i <= 39:
  94. f = (b ^ c ^ d)
  95. k = 0x6ED9EBA1
  96.  
  97. elif i <= 59:
  98. f = ((b & c) | (b & d) | (c & d))
  99. k = 0x8F1BBCDC
  100.  
  101. # elif i <= 79:
  102. else:
  103. f = (b ^ c ^ d)
  104. k = 0xCA62C1D6
  105.  
  106. temp = add32([leftrotate(a, 5), f, e, k, words[i]])
  107.  
  108. e = d
  109. d = c
  110. c = leftrotate(b, 30)
  111. b = a
  112. a = temp
  113.  
  114. h0 = add32([h0, a])
  115. h1 = add32([h1, b])
  116. h2 = add32([h2, c])
  117. h3 = add32([h3, d])
  118. h4 = add32([h4, e])
  119.  
  120. digest = (
  121. h0.to_bytes(4)
  122. + h1.to_bytes(4)
  123. + h2.to_bytes(4)
  124. + h3.to_bytes(4)
  125. + h4.to_bytes(4)
  126. )
  127.  
  128. return digest
  129.  
  130.  
  131. def main():
  132. m = "hello world"
  133. digest = sha1(m)
  134. print(hex(int.from_bytes(digest)))
  135.  
  136.  
  137. main()
  138.  
Advertisement
Add Comment
Please, Sign In to add comment