本文整理汇总了Python中cryptography.hazmat.primitives.hashes.Hash方法的典型用法代码示例。如果您正苦于以下问题:Python hashes.Hash方法的具体用法?Python hashes.Hash怎么用?Python hashes.Hash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cryptography.hazmat.primitives.hashes
的用法示例。
在下文中一共展示了hashes.Hash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _encrypted_data_keys_hash
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def _encrypted_data_keys_hash(hasher, encrypted_data_keys):
"""Generates the expected hash for the provided encrypted data keys.
:param hasher: Existing hasher to use
:type hasher: cryptography.hazmat.primitives.hashes.Hash
:param iterable encrypted_data_keys: Encrypted data keys to hash
:returns: Concatenated, sorted, list of all hashes
:rtype: bytes
"""
hashed_keys = []
for edk in encrypted_data_keys:
serialized_edk = serialize_encrypted_data_key(edk)
_hasher = hasher.copy()
_hasher.update(serialized_edk)
hashed_keys.append(_hasher.finalize())
return b"".join(sorted(hashed_keys))
# 512 bits of 0 for padding between hashes in decryption materials cache ID generation.
示例2: derive
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def derive(self, key_material):
if self._used:
raise AlreadyFinalized
self._used = True
if not isinstance(key_material, bytes):
raise TypeError("key_material must be bytes.")
output = [b""]
outlen = 0
counter = 1
while self._length > outlen:
h = hashes.Hash(self._algorithm, self._backend)
h.update(key_material)
h.update(_int_to_u32be(counter))
if self._sharedinfo is not None:
h.update(self._sharedinfo)
output.append(h.finalize())
outlen += len(output[-1])
counter += 1
return b"".join(output)[:self._length]
示例3: derive
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def derive(self, key_material):
if self._used:
raise AlreadyFinalized
self._used = True
utils._check_byteslike("key_material", key_material)
output = [b""]
outlen = 0
counter = 1
while self._length > outlen:
h = hashes.Hash(self._algorithm, self._backend)
h.update(key_material)
h.update(_int_to_u32be(counter))
if self._sharedinfo is not None:
h.update(self._sharedinfo)
output.append(h.finalize())
outlen += len(output[-1])
counter += 1
return b"".join(output)[:self._length]
示例4: check
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def check(self):
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
off = self.offset
if self.copyMethod == 0 and (1 << 20) > self.size >= 0x800+0xA00 and self.address == 0x08006000:
if self.sectionData[0x50 : 0x53] == b"K9L":
self.guessedType = self.sectionData[0x50 : 0x54].decode("ascii")
elif self.sectionData[0x50 : 0x54] == b"\xFF\xFF\xFF\xFF":
self.guessedType = "K9L0"
elif self.copyMethod == 0 and (1 << 20) > self.size >= 0xA00 and self.address == 0x08006800:
self.guessedType = "Kernel9"
elif self.copyMethod == 1 and self.size >= 0xA00:
if self.sectionData[0x100 : 0x104] == b"NCCH":
self.guessedType = "Kernel11 modules"
H = hashes.Hash(hashes.SHA256(), backend=default_backend())
H.update(self.sectionData)
self.hashIsValid = self.hash == H.finalize()
示例5: stretch
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def stretch(passw, iv1):
# hash the external iv and the password 8192 times
digest = iv1 + (16 * b"\x00")
for i in range(8192):
passHash = hashes.Hash(hashes.SHA256(), backend=default_backend())
passHash.update(digest)
passHash.update(bytes(passw, "utf_16_le"))
digest = passHash.finalize()
return digest
# encrypt file function
# arguments:
# infile: plaintext file path
# outfile: ciphertext file path
# passw: encryption password
# bufferSize: encryption buffer size, must be a multiple of
# AES block size (16)
# using a larger buffer speeds up things when dealing
# with big files
示例6: aes_encrypt
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def aes_encrypt(key, message):
#key = sys.argv[1].encode()
#plain = sys.argv[2].encode()
iv = os.urandom(16)
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(key.encode())
key_digest = digest.finalize()
cipher = Cipher(algorithms.AES(key_digest), modes.CFB(iv), backend=default_backend())
encryptor = cipher.encryptor()
encrypted = encryptor.update(message.encode()) + encryptor.finalize()
print(hexlify(iv).decode(), hexlify(encrypted).decode())
示例7: _legacy_pkcs1_v1_5_encode_md5_sha1
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def _legacy_pkcs1_v1_5_encode_md5_sha1(M, emLen):
"""
Legacy method for PKCS1 v1.5 encoding with MD5-SHA1 hash.
"""
M = bytes_encode(M)
md5_hash = hashes.Hash(_get_hash("md5"), backend=default_backend())
md5_hash.update(M)
sha1_hash = hashes.Hash(_get_hash("sha1"), backend=default_backend())
sha1_hash.update(M)
H = md5_hash.finalize() + sha1_hash.finalize()
if emLen < 36 + 11:
warning("pkcs_emsa_pkcs1_v1_5_encode: "
"intended encoded message length too short")
return None
PS = b'\xff' * (emLen - 36 - 3)
return b'\x00' + b'\x01' + PS + b'\x00' + H
#####################################################################
# Hash and padding helpers
#####################################################################
示例8: get_digest_and_size_for_file
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def get_digest_and_size_for_file(file_name):
"""Gets file digest and size.
Args:
file_name: Local path to a file.
Returns:
Tuple of file's digest and file size in bytes.
"""
use_openssl_only = os.getenv('SF_USE_OPENSSL_ONLY', 'False') == 'True'
CHUNK_SIZE = 16 * 4 * 1024
f = open(file_name, 'rb')
if not use_openssl_only:
m = SHA256.new()
else:
backend = default_backend()
chosen_hash = hashes.SHA256()
hasher = hashes.Hash(chosen_hash, backend)
while True:
chunk = f.read(CHUNK_SIZE)
if chunk == b'':
break
if not use_openssl_only:
m.update(chunk)
else:
hasher.update(chunk)
statinfo = os.stat(file_name)
file_size = statinfo.st_size
if not use_openssl_only:
digest = base64.standard_b64encode(m.digest()).decode(UTF8)
else:
digest = base64.standard_b64encode(hasher.finalize()).decode(UTF8)
logger = getLogger(__name__)
logger.debug('getting digest and size: %s, %s, file=%s', digest,
file_size, file_name)
return digest, file_size
示例9: thumbprint
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def thumbprint(self, hashalg=hashes.SHA256()):
"""Returns the key thumbprint as specified by RFC 7638.
:param hashalg: A hash function (defaults to SHA256)
"""
t = {'kty': self._params['kty']}
for name, val in iteritems(JWKValuesRegistry[t['kty']]):
if val.required:
t[name] = self._key[name]
digest = hashes.Hash(hashalg, backend=default_backend())
digest.update(bytes(json_encode(t).encode('utf8')))
return base64url_encode(digest.finalize())
示例10: salted_hmac
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def salted_hmac(key_salt, value, secret=None):
"""
Returns the HMAC-HASH of 'value', using a key generated from key_salt and a
secret (which defaults to settings.SECRET_KEY).
A different key_salt should be passed in for every application of HMAC.
:type key_salt: any
:type value: any
:type secret: any
:rtype: HMAC
"""
if secret is None:
secret = settings.SECRET_KEY
key_salt = force_bytes(key_salt)
secret = force_bytes(secret)
# We need to generate a derived key from our base key. We can do this by
# passing the key_salt and our base key through a pseudo-random function and
# SHA1 works nicely.
digest = hashes.Hash(
settings.CRYPTOGRAPHY_DIGEST, backend=settings.CRYPTOGRAPHY_BACKEND)
digest.update(key_salt + secret)
key = digest.finalize()
# If len(key_salt + secret) > sha_constructor().block_size, the above
# line is redundant and could be replaced by key = key_salt + secret, since
# the hmac module does the same thing for keys longer than the block size.
# However, we need to ensure that we *always* do this.
h = HMAC(
key,
settings.CRYPTOGRAPHY_DIGEST,
backend=settings.CRYPTOGRAPHY_BACKEND)
h.update(force_bytes(value))
return h
示例11: get_hash
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def get_hash(s):
digest = hashes.Hash(hashes.SHA256(), crypto_backend)
digest.update(s)
return digest.finalize().encode("hex").upper()
示例12: _build_hasher
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def _build_hasher(self):
"""Builds the hasher instance which will calculate the digest of all passed data.
:returns: Hasher object
"""
return hashes.Hash(self.algorithm.signing_hash_type(), backend=default_backend())
示例13: _new_cache_key_hasher
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def _new_cache_key_hasher():
"""Builds a new instance of the hasher used for building cache keys.
:rtype: cryptography.hazmat.primitives.hashes.Hash
"""
return hashes.Hash(hashes.SHA512(), backend=default_backend())
示例14: _partition_name_hash
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def _partition_name_hash(hasher, partition_name):
"""Generates the expected hash for the provided partition name.
:param hasher: Existing hasher to use
:type hasher: cryptography.hazmat.primitives.hashes.Hash
:param bytes partition_name: Partition name to hash
:returns: Complete hash
:rtype: bytes
"""
hasher.update(partition_name)
return hasher.finalize()
示例15: _encryption_context_hash
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import Hash [as 别名]
def _encryption_context_hash(hasher, encryption_context):
"""Generates the expected hash for the provided encryption context.
:param hasher: Existing hasher to use
:type hasher: cryptography.hazmat.primitives.hashes.Hash
:param dict encryption_context: Encryption context to hash
:returns: Complete hash
:rtype: bytes
"""
serialized_encryption_context = serialize_encryption_context(encryption_context)
hasher.update(serialized_encryption_context)
return hasher.finalize()