问题描述
from cryptography.fernet import Fernet
import base64
# Put this somewhere safe!
key = Fernet.generate_key()
f = Fernet()
token = f.encrypt(b"A really secret message. Not for prying eyes.")
token
print f.decrypt(token)
如何生成自己的密钥而不是 fernet.genrate_key()
?
How can I generate my own key instead of fernet.genrate_key()
?
推荐答案
在Fernet中,可以使用Fernet的关键派生函数
In fernet a key can be generated using one of fernet's Key Derivation Functions
fernet提供的功能之一是基于密码的密钥派生功能2".可以在与Fernet一起使用密码中找到使用PBKDF2HMAC的示例一个>.这在 pyca/cryptography的git问题#1333 中进行了讨论,maebert指出该示例使用salt = os.urandom(16),并且每次使用不同的salt值构造kdf类时,都会从密码生成一个新密钥.
One of the functions provided by fernet is the 'Password Based Key Derivation Function 2'.An example that uses PBKDF2HMAC can be found at Using Passwords with Fernet. This is discussed in git issue #1333 of pyca/cryptography, maebert points out that the example uses salt=os.urandom(16) and will generate a new key from a password each time the kdf class is constructed with a different salt value.
如果您需要使用自定义密钥派生功能,请查看 kdf 和 pbkdf2 具有实现KeyDerivationFunction接口的类的示例.
If you need to use a custom key derivation function look at source code for kdfand pbkdf2to have an example of a class that implements the KeyDerivationFunction interface.
匹配其签名并实现该接口的类应该能够作为自定义键派生函数插入.
A class that matches its signature and implements the interface should be able to be dropped in as a custom key derivation function.
这篇关于使用Python Fernet生成自己的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!