def __get_builtin_constructor(name):
if name in set(['sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
'SHA3_224', 'SHA3_256', 'SHA3_384', 'SHA3_512']):
bs = name[5:]
if bs == '224':
return sha3_224
elif bs == '256':
return sha3_256
elif bs == '384':
return sha3_384
elif bs == '512':
return sha3_512
return _hashlib_constructor(name)
python类sha3_256()的实例源码
def __init__(self, security_level=CURVE_P_256_Size, hash_algorithm=SHA2):
""" Init curve and hash function.
:param security_level: security level
:param hash_algorithm: hash function
"""
if security_level == CURVE_P_256_Size:
# order = openssl.backend._lib.BN_new()
# curve = openssl.backend._lib.EC_GROUP_new_by_curve_name(
# openssl.backend._lib.NID_X9_62_prime256v1)
# openssl.backend._lib.EC_GROUP_get_order(
# curve, order, openssl.backend._ffi.NULL)
self.order = int("115792089210356248762697446949407573529"
"996955224135760342422259061068512044369")
self.half_order = self.order >> 1
self.curve = ec.SECP256R1
self.sign_hash_algorithm = hashes.SHA256()
else:
# order = openssl.backend._lib.BN_new()
# curve = openssl.backend._lib.EC_GROUP_new_by_curve_name(
# openssl.backend._lib.NID_secp384r1)
# openssl.backend._lib.EC_GROUP_get_order(
# curve, order, openssl.backend._ffi.NULL)
self.order = int("39402006196394479212279040100"
"14361380507973927046544666794"
"69052796276593991132635693989"
"56308152294913554433653942643")
self.half_order = self.order >> 1
self.curve = ec.SECP384R1
self.sign_hash_algorithm = hashes.SHA384()
if hash_algorithm == SHA2:
self._hash = hashlib.sha256
elif hash_algorithm == SHA3 and security_level == CURVE_P_256_Size:
self._hash = hashlib.sha3_256
else:
self._hash = hashlib.sha3_384
def sha3_256(s):
import hashlib
import sys
if sys.version_info < (3, 4):
import sha3
return hashlib.sha3_256(s).digest()
def sha3_256(s):
import hashlib
import sys
if sys.version_info < (3, 4):
import sha3
return hashlib.sha3_256(s).digest()
def computeCryptoHash(data):
' This will currently return 128 hex characters'
# s = hashlib.sha3_256()
s = hashlib.sha256()
# s = hashlib.shake_256()
#return s.digest(64)
s.update(data)
return s.digest()
def setup(self, arg):
self.h = hashlib.sha3_256()
def sha3_256(message: bytes) -> bytes:
"""Generate SHA3-256 digest from message."""
return hashlib.sha3_256(message).digest()
def hash_chain(message: bytes) -> bytes:
"""Mix several hash functions to distribute trust.
This construction remains secure in case a weakness is discovered
in one of the hash functions (e.g. insecure algorithm that is not
unpredictable or that has weak preimage resistance, or if the
algorithm is badly implemented).
In case where the implementation is malicious, this construction
forces stateless implementations -- that try to compromise mixing
phase -- to guess it's position in the construction, which will
eventually lead to key state mismatch and thus detection.
"""
d1 = sha3_256(blake2s(sha256(message)))
d2 = sha3_256(sha256(blake2s(message)))
d3 = blake2s(sha3_256(sha256(message)))
d4 = blake2s(sha256(sha3_256(message)))
d5 = sha256(blake2s(sha3_256(message)))
d6 = sha256(sha3_256(blake2s(message)))
d7 = sha3_256(message)
d8 = blake2s(message)
d9 = sha256(message)
# Mixing phase
x1 = xor(d1, d2)
x2 = xor(x1, d3)
x3 = xor(x2, d4)
x4 = xor(x3, d5)
x5 = xor(x4, d6)
x6 = xor(x5, d7)
x7 = xor(x6, d8)
x8 = xor(x7, d9)
return x8