本文整理汇总了Python中cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC属性的典型用法代码示例。如果您正苦于以下问题:Python pbkdf2.PBKDF2HMAC属性的具体用法?Python pbkdf2.PBKDF2HMAC怎么用?Python pbkdf2.PBKDF2HMAC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cryptography.hazmat.primitives.kdf.pbkdf2
的用法示例。
在下文中一共展示了pbkdf2.PBKDF2HMAC属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_key
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_key():
if this.key:
return this.key
secret = getpass.getpass()
try:
salt = config().get('Security', 'salt')
except NoOptionError:
salt = base64.urlsafe_b64encode(os.urandom(16))
config().set('Security', 'salt', salt)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
this.key = base64.urlsafe_b64encode(kdf.derive(secret))
return this.key
示例2: _get_key
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_key(self, alg, key, p2s, p2c):
if isinstance(key, bytes):
plain = key
else:
plain = key.encode('utf8')
salt = bytes(self.name.encode('utf8')) + b'\x00' + p2s
if self.hashsize == 256:
hashalg = hashes.SHA256()
elif self.hashsize == 384:
hashalg = hashes.SHA384()
elif self.hashsize == 512:
hashalg = hashes.SHA512()
else:
raise ValueError('Unknown Hash Size')
kdf = PBKDF2HMAC(algorithm=hashalg, length=_inbytes(self.keysize),
salt=salt, iterations=p2c, backend=self.backend)
rk = kdf.derive(plain)
if _bitsize(rk) != self.keysize:
raise InvalidJWEKeyLength(self.keysize, len(rk))
return JWK(kty="oct", use="enc", k=base64url_encode(rk))
示例3: pbkdf2
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def pbkdf2(password, salt, iterations, dklen=0, digest=None):
"""
Implements PBKDF2 with the same API as Django's existing
implementation, using cryptography.
:type password: any
:type salt: any
:type iterations: int
:type dklen: int
:type digest: cryptography.hazmat.primitives.hashes.HashAlgorithm
"""
if digest is None:
digest = settings.CRYPTOGRAPHY_DIGEST
if not dklen:
dklen = digest.digest_size
password = force_bytes(password)
salt = force_bytes(salt)
kdf = PBKDF2HMAC(
algorithm=digest,
length=dklen,
salt=salt,
iterations=iterations,
backend=settings.CRYPTOGRAPHY_BACKEND)
return kdf.derive(password)
示例4: encrypt
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def encrypt(input_str, key, iterations=100000):
"""Basic encryption with a predefined key. Its purpose is to protect not very important data, just to avoid
saving them as plaintext."""
salt = b'D9\x82\xbfSibW(\xb1q\xeb\xd1\x84\x118'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=iterations,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(key.encode('utf-8')))
fer = Fernet(key)
h = fer.encrypt(input_str.encode('utf-8'))
h = h.hex()
return h
示例5: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def decrypt(input_str, key, iterations=100000):
try:
input_str = binascii.unhexlify(input_str)
salt = b'D9\x82\xbfSibW(\xb1q\xeb\xd1\x84\x118'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=iterations,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(key.encode('utf-8')))
fer = Fernet(key)
h = fer.decrypt(input_str)
h = h.decode('utf-8')
except:
raise
return h
示例6: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def decrypt(data, password):
"""Decrypts data using the password.
Decrypts the data using the provided password using the cryptography module.
If the pasword or data is incorrect this will return None.
"""
password = bytes(password)
#Salt is equal to password as we want the encryption to be reversible only
#using the password itself
kdf = PBKDF2HMAC(algorithm=hashes.AES(),
length=32,
salt=bytes(password),
iterations=100000,
backend=default_backend())
key = base64.urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
token = f.decrypt(data)
return token
示例7: _get_key
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_key(self, salt, password):
"""Returns an encryption key.
Args:
salt (bytes): a salt used during the encryption.
password (bytes): the password used to decrypt/encrypt
the message.
Returns:
Bytes with the encryption key.
"""
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=backends.default_backend()
)
return base64.urlsafe_b64encode(
kdf.derive(password))
示例8: __init__
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def __init__(self, key=None):
if key is None:
key = os.urandom(32)
if isinstance(key, str):
key_as_bytes = key.encode()
else:
key_as_bytes = key
salt_string = "constant so that session cookies carry across restarts of this app"
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt_string.encode(),
iterations=100000,
backend=default_backend(),
)
hashed_key = kdf.derive(key_as_bytes)
key_bytes = base64.encodebytes(hashed_key)
self.cipher = Fernet(key=key_bytes)
示例9: __derive_key
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def __derive_key(self, salt: bytes, iterations: int) -> bytes:
"""Derive a secret key from a given passphrase and salt."""
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=iterations,
backend=default_backend())
return urlsafe_b64encode(kdf.derive(self.passphrase.encode()))
示例10: get_encryptor
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def get_encryptor(salt, key):
generated_key = PBKDF2HMAC(algorithm=hashes.SHA256(),
length=32,
salt=bytes(salt, 'utf-8'),
iterations=2 ** 20,
backend=default_backend())
return Fernet(base64.urlsafe_b64encode(generated_key.derive(bytes(key, 'utf-8'))))
示例11: _get_encryptor
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_encryptor(self):
generated_key = PBKDF2HMAC(algorithm=hashes.SHA256(),
length=32,
salt=bytes(self.get_config('crypt_salt'), 'utf-8'),
iterations=2 ** 20,
backend=default_backend())
return Fernet(base64.urlsafe_b64encode(generated_key.derive(bytes(self.get_config('encryption_key'), 'utf-8'))))
示例12: pbkdf2_sha256
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def pbkdf2_sha256(password, salt, iters):
"""PBKDF2 with HMAC-SHA256"""
ctx = pbkdf2.PBKDF2HMAC(hashes.SHA256(), 32, salt, iters, default_backend())
return ctx.derive(password)
示例13: configure
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def configure(self):
backend = self.configured_data['BACKEND']
digest = self.configured_data['DIGEST']
salt = self.configured_data['SALT']
# Key Derivation Function
kdf = pbkdf2.PBKDF2HMAC(
algorithm=digest,
length=digest.digest_size,
salt=salt,
iterations=30000,
backend=backend,
)
self.configured_data['KEY'] = kdf.derive(
force_bytes(self.configured_data['KEY'] or settings.SECRET_KEY))
return self.configured_data
示例14: generatefernet
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def generatefernet(self):
"""Return a fernet object used to encrypt/decrypt the password"""
kdf = PBKDF2HMAC(
algorithm = hashes.SHA256(),
length = 32,
salt = self.salt,
iterations = 100000,
backend = default_backend())
key = base64.urlsafe_b64encode(kdf.derive(config.SALT.encode()))
return Fernet(key)
示例15: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def decrypt(message_encrypted, secret_key):
message_salt = message_encrypted[-24:]
message_payload = message_encrypted[:-24]
salt = base64.b64decode(message_salt)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=backend
)
key = base64.urlsafe_b64encode(kdf.derive(bytes(secret_key, encoding='utf8')))
f = Fernet(key)
return f.decrypt(bytes(message_payload, encoding='latin1')).decode('utf8')