本文整理汇总了Python中Crypto.Protocol.KDF.scrypt方法的典型用法代码示例。如果您正苦于以下问题:Python KDF.scrypt方法的具体用法?Python KDF.scrypt怎么用?Python KDF.scrypt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Protocol.KDF
的用法示例。
在下文中一共展示了KDF.scrypt方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _decode_keyfile_json_v3
# 需要导入模块: from Crypto.Protocol import KDF [as 别名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 别名]
def _decode_keyfile_json_v3(keyfile_json, password):
crypto = keyfile_json['crypto']
kdf = crypto['kdf']
# Derive the encryption key from the password using the key derivation
# function.
if kdf == 'pbkdf2':
derived_key = _derive_pbkdf_key(crypto, password)
elif kdf == 'scrypt':
derived_key = _derive_scrypt_key(crypto, password)
else:
raise TypeError("Unsupported key derivation function: {0}".format(kdf))
# Validate that the derived key matchs the provided MAC
ciphertext = decode_hex(crypto['ciphertext'])
mac = keccak(derived_key[16:32] + ciphertext)
expected_mac = decode_hex(crypto['mac'])
if not hmac.compare_digest(mac, expected_mac):
raise ValueError("MAC mismatch")
# Decrypt the ciphertext using the derived encryption key to get the
# private key.
encrypt_key = derived_key[:16]
cipherparams = crypto['cipherparams']
iv = big_endian_to_int(decode_hex(cipherparams['iv']))
private_key = decrypt_aes_ctr(ciphertext, encrypt_key, iv)
return private_key
#
# Key derivation
#
示例2: _scrypt_hash
# 需要导入模块: from Crypto.Protocol import KDF [as 别名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 别名]
def _scrypt_hash(password, salt, n, r, p, buflen):
derived_key = scrypt(
password,
salt=salt,
key_len=buflen,
N=n,
r=r,
p=p,
num_keys=1,
)
return derived_key
示例3: get_default_work_factor_for_kdf
# 需要导入模块: from Crypto.Protocol import KDF [as 别名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 别名]
def get_default_work_factor_for_kdf(kdf):
if kdf == 'pbkdf2':
return 1000000
elif kdf == 'scrypt':
return 262144
else:
raise ValueError("Unsupported key derivation function: {0}".format(kdf))
示例4: scrypt_hash
# 需要导入模块: from Crypto.Protocol import KDF [as 别名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 别名]
def scrypt_hash(
self, salt: str = "", key_length: int = 64, N: int = 14, r: int = 8, p: int = 1
):
"""Get Scrypt hash
scrypt is a password-based key derivation function (PBKDF) created by Colin Percival.
The algorithm was specifically designed to make it costly to perform large-scale
custom hardware attacks by requiring large amounts of memory. In 2016, the scrypt
algorithm was published by IETF as RFC 7914.
Args:
salt (str, optional): A string of characters that modifies the hash. Defaults to "".
key_length (int, optional): number of bytes to use when autogenerating new salts. Defaults to 64.
N (int, optional): CPU/memory cost parameter. Defaults to 14.
r (int, optional): The blocksize parameter. Defaults to 8.
p (int, optional): Parallelization parameter;. Defaults to 1.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("abc").scrypt_hash(salt="", key_length=16).out()
"f352f3374cf4e344dde4108b96985248"
"""
assert N < 32, "N must be less than 32"
self.state = _crypto_scrypt(
self._convert_to_bytes(), salt=salt, key_len=key_length, N=2 ** N, r=r, p=p
).hex()
return self
示例5: test2
# 需要导入模块: from Crypto.Protocol import KDF [as 别名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 别名]
def test2(self):
for tv in self.data:
try:
output = scrypt(tv.P, tv.S, tv.dkLen, tv.N, tv.r, tv.p)
except ValueError as e:
if " 2 " in str(e) and tv.N >= 1048576:
import warnings
warnings.warn("Not enough memory to unit test scrypt() with N=1048576", RuntimeWarning)
continue
else:
raise e
self.assertEqual(output, tv.output)
示例6: test3
# 需要导入模块: from Crypto.Protocol import KDF [as 别名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 别名]
def test3(self):
ref = scrypt(b("password"), b("salt"), 12, 16, 1, 1)
# Same output, but this time split over 2 keys
key1, key2 = scrypt(b("password"), b("salt"), 6, 16, 1, 1, 2)
self.assertEqual((ref[:6], ref[6:]), (key1, key2))
# Same output, but this time split over 3 keys
key1, key2, key3 = scrypt(b("password"), b("salt"), 4, 16, 1, 1, 3)
self.assertEqual((ref[:4], ref[4:8], ref[8:]), (key1, key2, key3))