Symmetric Encryption Utilities
decrypt(key, nonce, ciphertext, aad=None, base64_decode=False, return_str=False, warn_mode=WarnEnum.DEBUG)
Decrypts ciphertext using AES-GCM key and nonce.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
(bytes, required)
|
The decryption key. |
required |
nonce
|
(str | bytes, required)
|
The nonce used during encryption. |
required |
ciphertext
|
(str | bytes, required)
|
The data to be decrypted. |
required |
aad
|
str | bytes | None
|
Additional authenticated data. Defaults to None. |
None
|
base64_decode
|
bool
|
Whether to base64 decode the nonce and ciphertext. Defaults to False. |
False
|
return_str
|
bool
|
Whether to return the result as a string. Defaults to False. |
False
|
warn_mode
|
WarnEnum
|
The warning mode. Defaults to WarnEnum.DEBUG. |
DEBUG
|
Raises:
| Type | Description |
|---|---|
Exception
|
If failed to decrypt ciphertext using AES-GCM key and nonce for any reason. |
Returns:
| Type | Description |
|---|---|
str | bytes
|
str | bytes: The decrypted plaintext. |
Source code in src/potato_util/crypto/symmetric/aes_gcm.py
encrypt(key, plaintext, aad=None, nonce_nbytes=12, base64_encode=False, return_str=False, warn_mode=WarnEnum.DEBUG)
Encrypts plaintext using AES-GCM key and nonce.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
(bytes, required)
|
The encryption key. |
required |
plaintext
|
(str | bytes, required)
|
The data to be encrypted. |
required |
aad
|
str | bytes | None
|
Additional authenticated data. Defaults to None. |
None
|
nonce_nbytes
|
int
|
The number of bytes for the nonce. Defaults to 12. |
12
|
base64_encode
|
bool
|
Whether to base64 encode the nonce and ciphertext. Defaults to False. |
False
|
return_str
|
bool
|
Whether to return the result as a string. Defaults to False. |
False
|
warn_mode
|
WarnEnum
|
The warning mode. Defaults to WarnEnum.DEBUG. |
DEBUG
|
Raises:
| Type | Description |
|---|---|
Exception
|
If failed to encrypt plaintext using AES-GCM key and nonce for any reason. |
Returns:
| Type | Description |
|---|---|
tuple[str | bytes, str | bytes]
|
tuple[str | bytes, str | bytes]: The nonce and ciphertext resulting from the encryption. |
Source code in src/potato_util/crypto/symmetric/aes_gcm.py
gen_key(bit_length=256, return_str=False, base64_encode=True)
Generates a random AES-GCM key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bit_length
|
int
|
The length of the key in bits. Defaults to 256 (32 bytes). |
256
|
return_str
|
bool
|
Whether to return the key as a string. Defaults to False. |
False
|
base64_encode
|
bool
|
Whether to base64 encode the key if returning as a string. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
str | bytes
|
str | bytes: The generated AES-GCM key. |