def sha1_process(self): wblock = [] for x in xrange(80): wblock.append(0)
for x in xrange(16): wblock[x] = self.block[x]
for x in xrange(16, 80): wblock[x] = ROTL32(wblock[x - 3] ^ wblock[x - 8] ^ wblock[x - 14] ^ wblock[x - 16], 1) & 0xFFFFFFFF
a = self.hash_[0] b = self.hash_[1] c = self.hash_[2] d = self.hash_[3] e = self.hash_[4]
for x in xrange(20):
temp = ROTL32(a, 5) + (((c ^ d) & b) ^ d) + e + wblock[x] + 0x5A827999 temp &= 0xFFFFFFFF e = d d = c c = ROTL32(b, 30) & 0xFFFFFFFF b = a a = temp
for x in xrange(20, 40): temp = ROTL32(a, 5) + (b ^ c ^ d) + e + wblock[x] + 0x6ED9EBA1 temp &= 0xFFFFFFFF e = d d = c c = ROTL32(b, 30) & 0xFFFFFFFF b = a a = temp
for x in xrange(40, 60): temp = ROTL32(a, 5) + ((b & c) | (b & d) | (c & d)) + e + wblock[x] + 0x8F1BBCDC temp &= 0xFFFFFFFF e = d d = c c = ROTL32(b, 30) & 0xFFFFFFFF b = a a = temp
for x in xrange(60, 80): temp = ROTL32(a, 5) + (b ^ c ^ d) + e + wblock[x] + 0xCA62C1D6 temp &= 0xFFFFFFFF e = d d = c c = ROTL32(b, 30) & 0xFFFFFFFF b = a a = temp
self.hash_[0] += a self.hash_[1] += b self.hash_[2] += c self.hash_[3] += d self.hash_[4] += e for x in xrange(5): self.hash_[x] &= 0xFFFFFFFF
def str_to_block(self, x): self.block = [] for i in xrange(x, x + 64, 4): tmp = self.msg[i: i + 4] tmp = int(tmp.encode('hex') or '0', 16) self.block.append(tmp)
def final(self): for x in xrange(5): self.hash_[x] = ctypes.c_uint32(self.hash_[x]) result = "" for x in self.hash_: result += "{:0>8}".format(hex(x.value)[2:-1]) return result