Skip to content

encrypt

This module handles encryption logic

decrypt(payload)

Decrypts the given payload using a Fernet key.

Parameters:

Name Type Description Default
payload bytes

The encrypted data to be decrypted. Must be in bytes format.

required

Returns:

Type Description
bytes

The decrypted data in bytes format.

Source code in merlin/common/security/encrypt.py
def decrypt(payload: bytes) -> bytes:
    """
    Decrypts the given payload using a Fernet key.

    Args:
        payload: The encrypted data to be decrypted. Must be in bytes format.

    Returns:
        The decrypted data in bytes format.
    """
    key = _get_key()
    f = Fernet(key)
    del key
    return f.decrypt(payload)

encrypt(payload)

Encrypts the given payload using a Fernet key.

Parameters:

Name Type Description Default
payload bytes

The data to be encrypted. Must be in bytes format.

required

Returns:

Type Description
bytes

The encrypted data in bytes format.

Source code in merlin/common/security/encrypt.py
def encrypt(payload: bytes) -> bytes:
    """
    Encrypts the given payload using a Fernet key.

    Args:
        payload: The data to be encrypted. Must be in bytes format.

    Returns:
        The encrypted data in bytes format.
    """
    key = _get_key()
    f = Fernet(key)
    del key
    return f.encrypt(payload)

init_key()

Initializes the Fernet key and stores it on disk.

This function is called on import to prevent race conditions later on, or at least drastically reduce the number of corner cases where they could appear.

Source code in merlin/common/security/encrypt.py
def init_key():
    """
    Initializes the Fernet key and stores it on disk.

    This function is called on import to prevent race conditions later on,
    or at least drastically reduce the number of corner cases where they could appear.
    """
    Fernet(_get_key())