def test_update(self): pieces = [bchr(10) * 200, bchr(20) * 300] h = keccak.new(digest_bytes=64) h.update(pieces[0]).update(pieces[1]) digest = h.digest() h = keccak.new(digest_bytes=64) h.update(pieces[0] + pieces[1]) self.assertEqual(h.digest(), digest)
def test_new_positive(self): for digest_bits in (224, 256, 384, 512): hobj = keccak.new(digest_bits=digest_bits) self.assertEqual(hobj.digest_size, digest_bits // 8) hobj2 = hobj.new() self.assertEqual(hobj2.digest_size, digest_bits // 8) for digest_bytes in (28, 32, 48, 64): hobj = keccak.new(digest_bytes=digest_bytes) self.assertEqual(hobj.digest_size, digest_bytes) hobj2 = hobj.new() self.assertEqual(hobj2.digest_size, digest_bytes)
def test_update_after_digest(self): msg = b("rrrrttt") # Normally, update() cannot be done after digest() h = keccak.new(digest_bits=512, data=msg[:4]) dig1 = h.digest() self.assertRaises(TypeError, h.update, msg[4:]) dig2 = keccak.new(digest_bits=512, data=msg).digest() # With the proper flag, it is allowed h = keccak.new(digest_bits=512, data=msg[:4], update_after_digest=True) self.assertEqual(h.digest(), dig1) # ... and the subsequent digest applies to the entire message # up to that point h.update(msg[4:]) self.assertEqual(h.digest(), dig2)
def test_digest(self): h = keccak.new(digest_bytes=64) digest = h.digest() # hexdigest does not change the state self.assertEqual(h.digest(), digest) # digest returns a byte string self.assertTrue(isinstance(digest, type(b("digest"))))
def test_hex_digest(self): mac = keccak.new(digest_bits=512) digest = mac.digest() hexdigest = mac.hexdigest() # hexdigest is equivalent to digest self.assertEqual(hexlify(digest), tobytes(hexdigest)) # hexdigest does not change the state self.assertEqual(mac.hexdigest(), hexdigest) # hexdigest returns a string self.assertTrue(isinstance(hexdigest, type("digest")))
def test_new_negative(self): # keccak.new needs digest size self.assertRaises(TypeError, keccak.new) h = keccak.new(digest_bits=512) # Either bits or bytes can be specified self.assertRaises(TypeError, keccak.new, digest_bytes=64, digest_bits=512) # Range self.assertRaises(ValueError, keccak.new, digest_bytes=0) self.assertRaises(ValueError, keccak.new, digest_bytes=1) self.assertRaises(ValueError, keccak.new, digest_bytes=65) self.assertRaises(ValueError, keccak.new, digest_bits=0) self.assertRaises(ValueError, keccak.new, digest_bits=1) self.assertRaises(ValueError, keccak.new, digest_bits=513)
import os import string try: from crypto.Hash import keccak sha3_256 = lambda x: keccak.new(digest_bits=256, data=x).digest() except: import sha3 as _sha3 sha3_256 = lambda x: _sha3.sha3_256(x).digest() # from bitcoin import privtopub import sys import rlp from rlp.sedes import big_endian_int, BigEndianInt, Binary from rlp.utils import decode_hex, encode_hex, ascii_chr, str_to_bytes import random big_endian_to_int = lambda x: big_endian_int.deserialize( str_to_bytes(x).lstrip(b'\x00')) int_to_big_endian = lambda x: big_endian_int.serialize(x) TT256 = 2**256 TT256M1 = 2**256 - 1 TT255 = 2**255 if sys.version_info.major == 2: is_numeric = lambda x: isinstance(x, (int, long)) is_string = lambda x: isinstance(x, (str, unicode)) def to_string(value): return str(value)
def test_update_negative(self): h = keccak.new(digest_bytes=64) self.assertRaises(TypeError, h.update, "string")
def test_new_positive2(self): digest1 = keccak.new(data=b("\x90"), digest_bytes=64).digest() digest2 = keccak.new(digest_bytes=64).update(b("\x90")).digest() self.assertEqual(digest1, digest2)
def new_test(self, data=data, result=tv.md): hobj = keccak.new(digest_bits=512, data=data) self.assertEqual(hobj.digest(), result)