Skip to content

meringue.core.utils.crypt ¤

encrypt_message ¤

encrypt_message(msg: str) -> str

Method for encrypting a message with AES algorithm and the GCM method.

Attributes:

  • msg

    Message to Encrypt.

Returns:

  • str

    Encrypted message.

Source code in meringue/core/utils/crypt.py
def encrypt_message(msg: str) -> str:
    """
    Method for encrypting a message with AES algorithm and the GCM method.

    Attributes:
        msg: Message to Encrypt.

    Returns:
        Encrypted message.
    """

    cipher = AES.new(m_settings.CRYPTO_KEY.encode("utf8"), AES.MODE_GCM)
    encrypted_msg = cipher.encrypt(msg.encode("utf8"))
    nonce = cipher.nonce
    result = base64.urlsafe_b64encode(nonce + encrypted_msg).decode("utf-8")
    return result

decrypt_message ¤

decrypt_message(msg: str) -> str

Method for decrypting a message encrypted with AES algorithm and the GCM method.

Attributes:

  • msg

    Message to decrypt.

Returns:

  • str

    Decrypted message.

Source code in meringue/core/utils/crypt.py
def decrypt_message(msg: str) -> str:
    """
    Method for decrypting a message encrypted with AES algorithm and the GCM method.

    Attributes:
        msg: Message to decrypt.

    Returns:
        Decrypted message.
    """
    raw_msg = base64.urlsafe_b64decode(msg.encode("utf-8"))
    nonce, raw_msg = raw_msg[: AES.block_size], raw_msg[AES.block_size :]
    cipher = AES.new(m_settings.CRYPTO_KEY.encode("utf-8"), AES.MODE_GCM, nonce=nonce)
    decrypted_msg = cipher.decrypt(raw_msg).decode("utf-8")
    return decrypted_msg