def digest(key, msg, digest): """Fast inline implementation of HMAC key: key for the keyed hash object. msg: input message digest: A hash name suitable for hashlib.new() for best performance. *OR* A hashlib constructor returning a new hash object. *OR* A module supporting PEP 247. Note: key and msg must be a bytes or bytearray objects. """ if (_hashopenssl is not None and isinstance(digest, str) and digest in _openssl_md_meths): return _hashopenssl.hmac_digest(key, msg, digest) if callable(digest): digest_cons = digest elif isinstance(digest, str): digest_cons = lambda d=b'': _hashlib.new(digest, d) else: digest_cons = lambda d=b'': digest.new(d) inner = digest_cons() outer = digest_cons() blocksize = getattr(inner, 'block_size', 64) if len(key) > blocksize: key = digest_cons(key).digest() key = key + b'\x00' * (blocksize - len(key)) inner.update(key.translate(trans_36)) outer.update(key.translate(trans_5C)) inner.update(msg) outer.update(inner.digest()) return outer.digest()
def digest(key, msg, digest): """Fast inline implementation of HMAC. key: bytes or buffer, The key for the keyed hash object. msg: bytes or buffer, Input message. digest: A hash name suitable for hashlib.new() for best performance. *OR* A hashlib constructor returning a new hash object. *OR* A module supporting PEP 247. """ if _hashopenssl is not None and isinstance(digest, (str, _functype)): try: return _hashopenssl.hmac_digest(key, msg, digest) except _hashopenssl.UnsupportedDigestmodError: pass if callable(digest): digest_cons = digest elif isinstance(digest, str): digest_cons = lambda d=b'': _hashlib.new(digest, d) else: digest_cons = lambda d=b'': digest.new(d) inner = digest_cons() outer = digest_cons() blocksize = getattr(inner, 'block_size', 64) if len(key) > blocksize: key = digest_cons(key).digest() key = key + b'\x00' * (blocksize - len(key)) inner.update(key.translate(trans_36)) outer.update(key.translate(trans_5C)) inner.update(msg) outer.update(inner.digest()) return outer.digest()
def digest(key, msg, digest): if (_hashopenssl is not None and isinstance(digest, str) and digest in _openssl_md_meths): return _hashopenssl.hmac_digest(key, msg, digest) if callable(digest): digest_cons = digest elif isinstance(digest, str): digest_cons = lambda d=b'': _hashlib.new(digest, d) else: digest_cons = lambda d=b'': digest.new(d) inner = digest_cons() outer = digest_cons() blocksize = getattr(inner, 'block_size', 64) if len(key) > blocksize: key = digest_cons(key).digest() key = key + b'\x00' * (blocksize - len(key)) inner.update(key.translate(trans_36)) outer.update(key.translate(trans_5C)) inner.update(msg) outer.update(inner.digest()) return outer.digest()
def test(): for _ in range(9999): for alg in _hashlib.openssl_md_meth_names: _hashlib.hmac_digest(fstr(), fstr(), alg) try: _hashlib.pbkdf2_hmac(alg, fstr(), fstr(), fint()) _hashlib.pbkdf2_hmac(alg, fstr(), fstr(), fint(), fint()) except ValueError: pass try: _hashlib.scrypt(password=fstr(), salt=fstr(), n=fint(), r=fint(), p=fint()) except (ValueError, TypeError, AttributeError): pass try: do_hash_tests(_hashlib.new(alg)) do_hash_tests(_hashlib.new(alg, fstr())) except ValueError: pass hashes = [ _hashlib.openssl_md5, _hashlib.openssl_sha1, _hashlib.openssl_sha224, _hashlib.openssl_sha256, _hashlib.openssl_sha384, _hashlib.openssl_sha512, _md5.md5, _sha1.sha1, _sha256.sha224, _sha256.sha256, _sha512.sha384, _sha512.sha512, _sha3.sha3_224, _sha3.sha3_384, _sha3.sha3_512, _sha3.shake_128, _sha3.shake_256 ] for h in hashes: do_hash_tests(h()) do_hash_tests(h(fstr())) try: do_hash_tests( _blake2.blake2b(fstr(), digest_size=fint(), key=fstr(), salt=fstr(), person=fstr(), fanout=fint(), depth=fint(), leaf_size=fint(), node_offset=fint(), node_depth=fint(), inner_size=fint(), last_node=fint())) do_hash_tests( _blake2.blake2s(fstr(), digest_size=fint(), key=fstr(), salt=fstr(), person=fstr(), fanout=fint(), depth=fint(), leaf_size=fint(), node_offset=fint(), node_depth=fint(), inner_size=fint(), last_node=fint())) except (ValueError, OverflowError): pass