本文整理汇总了Python中cryptography.hazmat.primitives.kdf.scrypt.Scrypt方法的典型用法代码示例。如果您正苦于以下问题:Python scrypt.Scrypt方法的具体用法?Python scrypt.Scrypt怎么用?Python scrypt.Scrypt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cryptography.hazmat.primitives.kdf.scrypt
的用法示例。
在下文中一共展示了scrypt.Scrypt方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scrypt_generate
# 需要导入模块: from cryptography.hazmat.primitives.kdf import scrypt [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 别名]
def scrypt_generate(message):
backend = default_backend()
salt = os.urandom(16)
kdf = Scrypt(
salt=salt,
length=32,
n=2**14,
r=8,
p=1,
backend=backend
)
key = kdf.derive(message.encode())
print(hexlify(salt).decode(), hexlify(key).decode())
示例2: password_set
# 需要导入模块: from cryptography.hazmat.primitives.kdf import scrypt [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 别名]
def password_set(username, password):
backend = default_backend()
salt = os.urandom(16)
kdf = Scrypt(
salt=salt,
length=32,
n=2**14,
r=8,
p=1,
backend=backend
)
key = kdf.derive(password.encode())
conn = sqlite3.connect('db_users.sqlite')
conn.set_trace_callback(print)
conn.row_factory = sqlite3.Row
c = conn.cursor()
print('Changing password for', username)
c.execute("UPDATE users SET password = ?, salt = ? WHERE username = ?", (hexlify(key).decode(), hexlify(salt).decode(), username))
conn.commit()
示例3: scrypt
# 需要导入模块: from cryptography.hazmat.primitives.kdf import scrypt [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 别名]
def scrypt(passphrase, salt, scrypt_n=1<<13, scrypt_r=16, scrypt_p=1):
kdf = Scrypt(salt, length=32, n=scrypt_n, r=scrypt_r, p=scrypt_p, backend=default_backend())
return kdf.derive(passphrase)
示例4: login
# 需要导入模块: from cryptography.hazmat.primitives.kdf import scrypt [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 别名]
def login(username, password, **kwargs):
conn = sqlite3.connect('db_users.sqlite')
conn.set_trace_callback(print)
conn.row_factory = sqlite3.Row
c = conn.cursor()
user = c.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone()
if not user:
#print('The user doesnt exists')
return False
backend = default_backend()
kdf = Scrypt(
salt=unhexlify(user['salt']),
length=32,
n=2**14,
r=8,
p=1,
backend=backend
)
try:
kdf.verify(password.encode(), unhexlify(user['password']))
#print('valid')
return username
except InvalidKey:
#print('invalid1')
return False
except Exception as e:
#print('invalid2', e)
return False
#print('No deberia haber llegado aca')
return False
示例5: crack_scrypt
# 需要导入模块: from cryptography.hazmat.primitives.kdf import scrypt [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 别名]
def crack_scrypt(key, salt):
try:
salt = unhexlify(sys.argv[1].encode())
key = unhexlify(sys.argv[2].encode())
except binascii.Error:
print('Non-hexadecimal data on salt and/or key', file=sys.stderr)
return False
backend = default_backend()
for number in range(10000):
kdf = Scrypt(
salt=salt,
length=32,
n=2**14,
r=8,
p=1,
backend=backend
)
try:
kdf.verify(str(number).encode(), key)
print('Cracked! Password:', number)
break
except InvalidKey:
pass
示例6: __call__
# 需要导入模块: from cryptography.hazmat.primitives.kdf import scrypt [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 别名]
def __call__(self,
password: bytes,
salt: bytes,
**kwargs) -> bytes:
"""
Derives a symmetric encryption key from a pair of password and salt.
It also accepts an additional _scrypt_cost argument.
WARNING: RFC7914 recommends that you use a 2^20 cost value for sensitive
files. It is NOT recommended to change the `_scrypt_cost` value unless
you know what you are doing.
:param password: byte-encoded password used to derive a symmetric key
:param salt: cryptographic salt added during key derivation
:return:
"""
_scrypt_cost = kwargs.get('_scrypt_cost', Scrypt.__DEFAULT_SCRYPT_COST)
try:
derived_key = CryptographyScrypt(
salt=salt,
length=SecretBox.KEY_SIZE,
n=2 ** _scrypt_cost,
r=8,
p=1,
backend=default_backend()
).derive(password)
except InternalError as e:
required_memory = 128 * 2**_scrypt_cost * 8 // (10**6)
if e.err_code[0].reason == 65:
raise MemoryError(
"Scrypt key derivation requires at least {} MB of memory. "
"Please free up some memory and try again.".format(required_memory)
)
else:
raise e
else:
return derived_key
示例7: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.kdf import scrypt [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 别名]
def decrypt(self, jsons):
"""Import file is AES GCM encrypted, let's decrypt it.
Based on the import script from Aegis:
https://github.com/beemdevelopment/Aegis/blob/master/scripts/decrypt.py
Format documentation:
https://github.com/beemdevelopment/Aegis/blob/master/docs/vault.md
"""
if not CRYPTOGRAPHY:
raise ImportError(name='cryptography')
password = getpassword(self.prefix)
master_key = None
for slot in jsons['header']['slots']:
if slot['type'] != 1:
continue
kdf = Scrypt(salt=bytes.fromhex(slot['salt']),
length=32,
n=slot['n'],
r=slot['r'],
p=slot['p'],
backend=default_backend())
key = kdf.derive(password.encode("utf-8"))
cipher = AESGCM(key)
param = slot['key_params']
try:
nonce = bytes.fromhex(param['nonce'])
data = bytes.fromhex(slot['key']) + bytes.fromhex(param['tag'])
master_key = cipher.decrypt(nonce=nonce,
data=data,
associated_data=None)
except InvalidTag: # pragma: no cover
pass
if master_key is None: # pragma: no cover
raise FormatError("unable to decrypt the master key.")
cipher = AESGCM(master_key)
param = jsons['header']['params']
content = base64.b64decode(jsons['db']) + bytes.fromhex(param['tag'])
plain = cipher.decrypt(nonce=bytes.fromhex(param['nonce']),
data=content,
associated_data=None)
return plain.decode('utf-8')