def fernet_encrypt_psk(message, raw=False):
"""Encrypts the specified message using the Fernet format.
Returns the encrypted token, as a byte string.
Note that a Fernet token includes the current time. Users decrypting a
the token can specify a TTL (in seconds) indicating how long the encrypted
message should be valid. So the system clock must be correct before calling
this function.
:param message: The message to encrypt.
:type message: Must be of type 'bytes' or a UTF-8 'str'.
:param raw: if True, returns the decoded base64 bytes representing the
Fernet token. The bytes must be converted back to base64 to be
decrypted. (Or the 'raw' argument on the corresponding
fernet_decrypt_psk() function can be used.)
:return: the encryption token, as a base64-encoded byte string.
"""
fernet = _get_fernet_context()
if isinstance(message, str):
message = message.encode("utf-8")
token = fernet.encrypt(message)
if raw is True:
token = urlsafe_b64decode(token)
return token
评论列表
文章目录