Facebook
From xD, 4 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 66
  1. import hashlib
  2.  
  3. class sha256:
  4.  
  5.         BITS_IN_WORD = 32          #Number of bits in word
  6.  
  7.         #definition
  8.         K = (0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  9.              0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  10.              0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  11.              0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  12.              0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  13.              0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  14.              0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  15.              0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  16.              0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  17.              0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  18.              0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  19.              0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  20.              0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  21.              0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  22.              0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  23.              0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2)
  24.  
  25.         #Initial values
  26.         H = (0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  27.              0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19)
  28.  
  29.         def __init__(self, input_text):
  30.             if type(input_text) is not str:
  31.                 raise TypeError("Argument has to be string\n")
  32.             else:
  33.                 self.OUTPUT_TEXT = ''.join('{:02x}'.format(i) for i in self.process(input_text))
  34.  
  35.         def rotateright(self,x, y):
  36.             return ((x >> y) | (x << (self.BITS_IN_WORD - y))) & 0xFFFFFFFF
  37.  
  38.         def pad(self,x):
  39.             mdi = len(x) % 64
  40.             L = (len(x) << 3).to_bytes(8, 'big')
  41.             npad = 55 - mdi if mdi < 56 else 119 - mdi
  42.             return bytes(x, 'ascii') + b'\x80' + (b'\x00' * npad) + L
  43.  
  44.         def compress(self,wt, kt, a, b, c, d, e, f, g, h):
  45.             ch = (e & f) ^ (~e & g)
  46.             ma = (a & b) ^ (a & c) ^ (b & c)        #Major
  47.             s0 = self.rotateright(a, 2) ^ self.rotateright(a, 13) ^ self.rotateright(a, 22)
  48.             s1 = self.rotateright(e, 6) ^ self.rotateright(e, 11) ^ self.rotateright(e, 25)
  49.             t1 = h + s1 + ch + wt + kt
  50.             return (t1 + s0 + ma) & 0xFFFFFFFF, a, b, c, (d + t1) & 0xFFFFFFFF, e, f, g
  51.  
  52.         def process(self, text):
  53.             padded = self.pad(text)
  54.             digest = list(self.H)
  55.             for j in range(0, len(padded), 64):
  56.                 current = padded[j:j + 64]
  57.                 arr = [0] * 64
  58.                 arr[0:16] = [int.from_bytes(current[i:i + 4], 'big') for i in range(0, 64, 4)]
  59.                 for i in range(16, 64):
  60.                     s0 = self.rotateright(arr[i - 15], 7) ^ self.rotateright(arr[i - 15], 18) ^ (arr[i - 15] >> 3)
  61.                     s1 = self.rotateright(arr[i - 2], 17) ^ self.rotateright(arr[i - 2], 19) ^ (arr[i - 2] >> 10)
  62.                     arr[i] = (arr[i - 16] + s0 + arr[i-7] + s1) & 0xFFFFFFFF
  63.                 a, b, c, d, e, f, g, h = digest
  64.                 for i in range(64):
  65.                     a, b, c, d, e, f, g, h = self.compress(arr[i], self.K[i], a, b, c, d, e, f, g, h)
  66.                 digest = [(X + Y) & 0xFFFFFFFF for X, Y in zip(digest, (a, b, c, d, e, f, g, h))]
  67.             return b''.join(Di.to_bytes(4, 'big') for Di in digest)  #byte array
  68.  
  69.         def hexdigest(self):
  70.             return self.OUTPUT_TEXT
  71.  
  72.  
  73. def main():
  74.     print(sha256("x222D").hexdigest())
  75.     print(hashlib.sha256(b'x222D').hexdigest())
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     main()
  80.