pgcontents/crypto.py
def derive_single_fernet_key(password, user_id):
    """
    Convert a secret key and a user ID into an encryption key to use with a
    ``cryptography.fernet.Fernet``.

    Taken from
    https://cryptography.io/en/latest/fernet/#using-passwords-with-fernet

    Parameters
    ----------
    password : unicode
        ascii-encodable key to derive
    user_id : unicode
        ascii-encodable user_id to use as salt
    """
    password = ascii_unicode_to_bytes(password)
    user_id = ascii_unicode_to_bytes(user_id)

    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=user_id,
        iterations=100000,
        backend=default_backend(),
    )
    return base64.urlsafe_b64encode(kdf.derive(password))
def derive_single_fernet_key(password, user_id):
    """
    Convert a secret key and a user ID into an encryption key to use with a
    ``cryptography.fernet.Fernet``.

    Taken from
    https://cryptography.io/en/latest/fernet/#using-passwords-with-fernet

    Parameters
    ----------
    password : unicode
        ascii-encodable key to derive
    user_id : unicode
        ascii-encodable user_id to use as salt
    """
    password = ascii_unicode_to_bytes(password)
    user_id = ascii_unicode_to_bytes(user_id)

    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=user_id,
        iterations=100000,
        backend=default_backend(),
    )
    return base64.urlsafe_b64encode(kdf.derive(password))
Convert a secret key and a user ID into an encryption key to use with a
    ``cryptography.fernet.Fernet``.

    Taken from
    https://cryptography.io/en/latest/fernet/#using-passwords-with-fernet

    Parameters
    ----------
    password : unicode
        ascii-encodable key to derive
    user_id : unicode
        ascii-encodable user_id to use as salt