Asymmetric Encryption Utilities
async_create_keys(asymmetric_keys_dir, key_size, private_key_fname, public_key_fname, force=False, warn_mode=WarnEnum.DEBUG)
async
Async generate and create asymmetric key files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asymmetric_keys_dir
|
(str, required)
|
Asymmetric keys directory. |
required |
key_size
|
(int, required)
|
Asymmetric key size. |
required |
private_key_fname
|
(str, required)
|
Asymmetric private key filename. |
required |
public_key_fname
|
(str, required)
|
Asymmetric public key filename. |
required |
force
|
bool
|
Force to create asymmetric keys. Defaults to False. |
False
|
warn_mode
|
WarnEnum
|
Warning mode. Defaults to WarnEnum.DEBUG. |
DEBUG
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
If warning mode is ERROR and asymmetric keys already exist. |
OSError
|
If failed to create asymmetric keys. |
Source code in src/potato_util/crypto/asymmetric/_async.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
async_get_keys(private_key_path, public_key_path, as_str=False)
async
Async read asymmetric keys from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key_path
|
(str, required)
|
Asymmetric private key path. |
required |
public_key_path
|
(str, required)
|
Asymmetric public key path. |
required |
as_str
|
bool
|
Return keys as strings. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[RSAPrivateKey | str, RSAPublicKey | str]
|
tuple[RSAPrivateKey | str, RSAPublicKey | str]: Private and public keys. |
Source code in src/potato_util/crypto/asymmetric/_async.py
async_get_private_key(private_key_path, as_str=False)
async
Async read asymmetric private key from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key_path
|
(str, required)
|
Asymmetric private key path. |
required |
as_str
|
bool
|
Return private key as string. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If Asymmetric private key file not found. |
Returns:
| Type | Description |
|---|---|
RSAPrivateKey | str
|
RSAPrivateKey | str: Asymmetric private key. |
Source code in src/potato_util/crypto/asymmetric/_async.py
async_get_public_key(public_key_path, as_str=False)
async
Async read asymmetric public key from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
public_key_path
|
(str, required)
|
Asymmetric public key path. |
required |
as_str
|
bool
|
Return public key as string. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If asymmetric public key file not found. |
Returns:
| Type | Description |
|---|---|
RSAPublicKey | str
|
RSAPublicKey | str: Asymmetric public key. |
Source code in src/potato_util/crypto/asymmetric/_async.py
create_keys(asymmetric_keys_dir, key_size, private_key_fname, public_key_fname, force=False, warn_mode=WarnEnum.DEBUG)
Generate and create asymmetric key files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asymmetric_keys_dir
|
(str, required)
|
Asymmetric keys directory. |
required |
key_size
|
(int, required)
|
Asymmetric key size. |
required |
private_key_fname
|
(str, required)
|
Asymmetric private key filename. |
required |
public_key_fname
|
(str, required)
|
Asymmetric public key filename. |
required |
force
|
bool
|
Force to create asymmetric keys. Defaults to False. |
False
|
warn_mode
|
WarnEnum
|
Warning mode. Defaults to WarnEnum.DEBUG. |
DEBUG
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
If warning mode is ERROR and asymmetric keys already exist. |
OSError
|
If failed to create asymmetric keys. |
Source code in src/potato_util/crypto/asymmetric/_sync.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
decrypt_with_private_key(ciphertext, private_key, base64_decode=False, as_str=False, warn_mode=WarnEnum.DEBUG)
Decrypt ciphertext with private key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ciphertext
|
(str | bytes, required)
|
Ciphertext to decrypt. |
required |
private_key
|
(RSAPrivateKey, required)
|
Private key. |
required |
base64_decode
|
bool
|
Decode ciphertext with base64. Defaults to False. |
False
|
as_str
|
bool
|
Return plaintext as string or bytes. Defaults to False. |
False
|
warn_mode
|
WarnEnum
|
Warning mode. Defaults to WarnEnum.DEBUG. |
DEBUG
|
Raises:
| Type | Description |
|---|---|
Exception
|
If failed to decrypt ciphertext with asymmetric private key for any reason. |
Returns:
| Type | Description |
|---|---|
str | bytes
|
str | bytes: Decrypted plaintext as string or bytes. |
Source code in src/potato_util/crypto/asymmetric/_base.py
encrypt_with_public_key(plaintext, public_key, base64_encode=False, as_str=False, warn_mode=WarnEnum.DEBUG)
Encrypt plaintext with public key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plaintext
|
(str | bytes, required)
|
Plaintext to encrypt. |
required |
public_key
|
(RSAPublicKey, required)
|
Public key. |
required |
base64_encode
|
bool
|
Encode ciphertext with base64. Defaults to False. |
False
|
as_str
|
bool
|
Return ciphertext as string or bytes. Defaults to False. |
False
|
warn_mode
|
WarnEnum
|
Warning mode. Defaults to WarnEnum.DEBUG. |
DEBUG
|
Raises:
| Type | Description |
|---|---|
Exception
|
If failed to encrypt plaintext with asymmetric public key. |
Returns:
| Type | Description |
|---|---|
str | bytes
|
str | bytes: Encrypted ciphertext as string or bytes. |
Source code in src/potato_util/crypto/asymmetric/_base.py
gen_key_pair(key_size, as_str=False)
Generate RSA key pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_size
|
(int, required)
|
RSA key size. |
required |
as_str
|
bool
|
Return keys as strings. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[RSAPrivateKey | str, RSAPublicKey | str]
|
tuple[RSAPrivateKey | str, RSAPublicKey | str]: RSA private and public keys. |
Source code in src/potato_util/crypto/asymmetric/_base.py
get_keys(private_key_path, public_key_path, as_str=False)
Read asymmetric keys from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key_path
|
(str, required)
|
Asymmetric private key path. |
required |
public_key_path
|
(str, required)
|
Asymmetric public key path. |
required |
as_str
|
bool
|
Return keys as strings. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[RSAPrivateKey | str, RSAPublicKey | str]
|
tuple[RSAPrivateKey | str, RSAPublicKey | str]: Private and public keys. |
Source code in src/potato_util/crypto/asymmetric/_sync.py
get_private_key(private_key_path, as_str=False)
Read asymmetric private key from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key_path
|
(str, required)
|
Asymmetric private key path. |
required |
as_str
|
bool
|
Return private key as string. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If asymmetric private key file not found. |
Returns:
| Type | Description |
|---|---|
RSAPrivateKey | str
|
RSAPrivateKey | str: Asymmetric private key as RSAPrivateKey or str. |
Source code in src/potato_util/crypto/asymmetric/_sync.py
get_public_key(public_key_path, as_str=False)
Read asymmetric public key from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
public_key_path
|
(str, required)
|
Asymmetric public key path. |
required |
as_str
|
bool
|
Return public key as string. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If asymmetric public key file not found. |
Returns:
| Type | Description |
|---|---|
RSAPublicKey | str
|
RSAPublicKey | str: Asymmetric public key as RSAPublicKey or str. |