Пример #1
0
 def __call__(self, key: bytes, data: bytes) -> bytes:
     # padding key
     if len(key) > self._b // 8:
         key = self._hash_func(key).digest
     key = bitarray.from_bytes(key)
     k = bitarray.concat((key, bitarray(0, self._b - len(key))))
     # process data
     data = bitarray.from_bytes(data)
     si = k ^ self._ipad
     data = bitarray.concat((si, data))
     data = self._hash(data)
     so = k ^ self._opad
     data = bitarray.concat((so, data))
     data = self._hash(data)
     return Digest(data.to_bytes())
Пример #2
0
 def __call__(self, data: bytes) -> Digest:
     data = bitarray.from_bytes(data)
     chunks = self._padding(data)
     h0, h1, h2, h3, h4 = self._h
     for chunk in chunks:
         w = chunk.split(32)
         for i in range(16, 80):  # 32 words -> 80 words
             w.append((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]) << 1)
         a, b, c, d, e = h0, h1, h2, h3, h4
         for i in range(80):
             if 0 <= i <= 19:
                 f = (b & c) | ((~b) & d)
                 k = bitarray(0x5A827999, 32)
             elif 20 <= i <= 39:
                 f = b ^ c ^ d
                 k = bitarray(0x6ED9EBA1, 32)
             elif 40 <= i <= 59:
                 f = (b & c) | (b & d) | (c & d)
                 k = bitarray(0x8F1BBCDC, 32)
             elif 60 <= i <= 79:
                 f = b ^ c ^ d
                 k = bitarray(0xCA62C1D6, 32)
             temp = (a << 5) + f + e + k + w[i]
             a, b, c, d, e = temp, a, b << 30, c, d
         h0, h1, h2, h3, h4 = h0 + a, h1 + b, h2 + c, h3 + d, h4 + e
     digest = bitarray.concat((h0, h1, h2, h3, h4))
     return Digest(digest.to_bytes())
Пример #3
0
 def _hash(self, data: bitarray) -> bitarray:
     data = self._hash_func(data.to_bytes()).digest
     return bitarray.from_bytes(data)
Пример #4
0
 def _bytes2bits(self, b: bytes) -> bitarray:  # 3.2.5
     return bitarray.from_bytes(b)
Пример #5
0
 def __call__(self, data: bytes) -> bytes:
     data = bitarray.from_bytes(data)
     data = self._sponge(data, self._d)
     return Digest(data.to_bytes())