Helpers
helpers.aes
helpers.aes
This module provides a class for encrypting and decrypting data using AES in GCM mode.
AESHelper
Source code in helpers/aes.py
13 14 15 16 17 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 | |
__init__(key)
Initialize the AESHelper instance with the provided key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
bytes | str
|
The key to use for encryption and decryption. |
required |
Returns:
| Type | Description |
|---|---|
|
The hex-encoded AES key. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the key is not a string or bytes. |
ValueError
|
If the key is not a string or bytes and cannot be converted to bytes. |
Source code in helpers/aes.py
decrypt(encrypted_data)
Decrypt data using AES in GCM mode. Extracts the nonce, tag, and ciphertext from the encrypted data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encrypted_data
|
bytes
|
The encrypted data to decrypt. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The decrypted data. |
Source code in helpers/aes.py
encrypt(data)
Encrypt data using AES in GCM mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The data to encrypt. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The ciphertext, which includes the nonce and authentication tag. |
Source code in helpers/aes.py
get_random_key(size=16)
staticmethod
Generate a random AES key of the specified size (16, 24, or 32 bytes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
Literal[16, 24, 32]
|
The size of the key to generate. |
16
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The generated key as a hex-encoded string. |
Source code in helpers/aes.py
try_encode_str(key)
staticmethod
Try to convert a string key to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The string key to convert. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
bytes | None: The converted key or None if conversion fails. |
Source code in helpers/aes.py
helpers.ecdh
helpers.ecdh
This module provides a class for Elliptic-curve Diffie-Hellman (ECDH) key exchange operations, including key pair generation, serialization/deserialization of keys, shared secret generation, and data encryption/decryption.
ECDHHelper
Helper class for ECDH key exchange operations, including key pair generation, serialization/deserialization of keys, shared secret generation, and data encryption/decryption.
Source code in helpers/ecdh.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
decrypt_data(ciphertext, shared_secret)
staticmethod
Decrypts data using AES encryption with the given shared secret.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ciphertext
|
bytes
|
Encrypted data (ciphertext). |
required |
shared_secret
|
bytes
|
Shared secret used for decryption. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Decrypted data. |
Source code in helpers/ecdh.py
deserialize_private_key(data)
staticmethod
Deserializes a PEM-encoded private key from bytes or hex string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes | str
|
Serialized private key in bytes or hex string format. |
required |
Returns:
| Type | Description |
|---|---|
|
ec.EllipticCurvePrivateKey: Elliptic curve private key object. |
Source code in helpers/ecdh.py
deserialize_public_key(data)
staticmethod
Deserializes a PEM-encoded public key from bytes or hex string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes | str
|
Serialized public key in bytes or hex string format. |
required |
Returns:
| Type | Description |
|---|---|
|
ec.EllipticCurvePublicKey: Elliptic curve public key object. |
Source code in helpers/ecdh.py
encrypt_data(data, shared_secret)
staticmethod
Encrypts data using AES encryption with the given shared secret.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to encrypt. |
required |
shared_secret
|
bytes
|
Shared secret used for encryption. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Encrypted data (ciphertext). |
Source code in helpers/ecdh.py
generate_key_pair(out_format=KeyFormat.OBJECT)
staticmethod
Generates an elliptic curve key pair in the specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_format
|
KeyFormat | int
|
Desired output format for the key pair (OBJECT, BYTE, STRING). |
OBJECT
|
Returns:
| Name | Type | Description |
|---|---|---|
TKeyPair |
TKeyPair
|
Key pair in the specified format. |
Source code in helpers/ecdh.py
generate_shared_secret(private_key, public_key)
staticmethod
Generates a shared secret using ECDH key exchange.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key
|
EllipticCurvePrivateKey
|
Elliptic curve private key. |
required |
public_key
|
EllipticCurvePublicKey
|
Elliptic curve public key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Shared secret bytes. |
Source code in helpers/ecdh.py
serialize_private_key(private_key, return_type=bytes)
staticmethod
Serializes a private key to PEM format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key
|
EllipticCurvePrivateKey
|
Elliptic curve private key. |
required |
return_type
|
type[StrBytes]
|
Whether to return the serialized key as a string or bytes. |
bytes
|
Returns:
| Name | Type | Description |
|---|---|---|
StrBytes |
StrBytes
|
Serialized private key in PEM format. |
Source code in helpers/ecdh.py
serialize_public_key(public_key, return_type=bytes)
staticmethod
Serializes a public key to PEM format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
public_key
|
EllipticCurvePublicKey
|
Elliptic curve public key. |
required |
return_type
|
type[StrBytes]
|
Whether to return the serialized key as a string or bytes. |
bytes
|
Returns:
| Name | Type | Description |
|---|---|---|
StrBytes |
StrBytes
|
Serialized public key in PEM format. |
Source code in helpers/ecdh.py
helpers.jwt_token
helpers.jwt_token
create_access_token(data, expires_delta=timedelta(minutes=JWT_EXPIRE_MINUTES))
Generates a JWT token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, str]
|
The payload of the JWT token. |
required |
expires_delta
|
timedelta
|
The time delta for token expiration. Defaults to timedelta(minutes=JWT_EXPIRE_MINUTES). |
timedelta(minutes=JWT_EXPIRE_MINUTES)
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated JWT token. |
Source code in helpers/jwt_token.py
decode_access_token(token)
Decodes and validates a JWT token. Returns the username and password.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token to decode. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
tuple[str, str]: A tuple containing the username and password. |
Source code in helpers/jwt_token.py
helpers.rsa
helpers.rsa
This module provides a class for RSA key exchange operations, including key pair generation, serialization/deserialization of keys, and data encryption/decryption.
RSAHelper
Helper class for RSA key exchange operations, including key pair generation, serialization/deserialization of keys, and data encryption/decryption.
Source code in helpers/rsa.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
decrypt_data(ciphertext, private_key)
staticmethod
Decrypt data using AES encryption with the given shared secret.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ciphertext
|
bytes
|
Encrypted data (ciphertext) |
required |
private_key
|
RSAPrivateKey | str | bytes
|
Private key used for decryption, can be an RSAPrivateKey object, a PEM string, or bytes |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Decrypted data |
Source code in helpers/rsa.py
deserialize_private_key(data)
staticmethod
Deserializes a PEM-encoded private key from bytes or hex string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes | str
|
Serialized private key in bytes or hex string format. |
required |
Returns:
| Type | Description |
|---|---|
RSAPrivateKey
|
rsa.RSAPrivateKey: Elliptic curve private key object. |
Source code in helpers/rsa.py
deserialize_public_key(data)
staticmethod
Deserializes a PEM-encoded public key from bytes or hex string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes | str
|
Serialized public key in bytes or hex string format |
required |
Returns:
| Type | Description |
|---|---|
RSAPublicKey
|
rsa.RSAPublicKey: RSA public key object |
Source code in helpers/rsa.py
encrypt_data(data, public_key)
staticmethod
Encrypts data using AES encryption with the given shared secret.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to encrypt |
required |
public_key
|
RSAPublicKey | str | bytes
|
Public key used for encryption, can be an RSAPublicKey object, a PEM string, or bytes |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Encrypted data (ciphertext) |
Source code in helpers/rsa.py
generate_key_pair(out_format=KeyFormat.OBJECT)
staticmethod
Generates an RSA key pair in the specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_format
|
KeyFormat
|
Desired output format for the key pair (OBJECT, BYTE, STRING) |
OBJECT
|
Returns:
| Name | Type | Description |
|---|---|---|
TKeyPair |
TKeyPair
|
Key pair in the specified format |
Source code in helpers/rsa.py
get_padding()
staticmethod
Returns an OAEP padding object configured with MGF1 using SHA-256 as the hash algorithm. This padding is used for RSA encryption to provide additional security measures, such as preventing chosen ciphertext attacks. No label is used in this configuration.
Returns:
| Type | Description |
|---|---|
OAEP
|
padding.OAEP: OAEP padding object configured with MGF1 using SHA-256 |
Source code in helpers/rsa.py
serialize_private_key(private_key, return_type=bytes)
staticmethod
Serializes a private key to PEM format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key
|
RSAPrivateKey
|
RSA private key |
required |
return_type
|
type[StrBytes]
|
Whether to return the serialized key as a string or bytes |
bytes
|
Returns:
| Name | Type | Description |
|---|---|---|
StrBytes |
StrBytes
|
Serialized private key in PEM format as a string or bytes |
Source code in helpers/rsa.py
serialize_public_key(public_key, return_type=bytes)
staticmethod
Serializes a public key to PEM format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
public_key
|
RSAPublicKey
|
RSA public key |
required |
return_type
|
type[StrBytes]
|
Desired return type for the serialized key (str or bytes) |
bytes
|
Returns:
| Name | Type | Description |
|---|---|---|
StrBytes |
StrBytes
|
Serialized public key in PEM format as a string or bytes |
Source code in helpers/rsa.py
sign_data(data, private_key)
staticmethod
Signs the given data using the provided private key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to be signed |
required |
private_key
|
RSAPrivateKey | str | bytes
|
Private key used for signing, can be an RSAPrivateKey object, a PEM string, or bytes |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The generated signature as bytes |
Source code in helpers/rsa.py
verify_key_pair(private_key, public_key)
staticmethod
Verifies that the given private and public key are a matching pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
private_key
|
RSAPrivateKey | str | bytes
|
Private key to verify, can be an RSAPrivateKey object, a PEM string, or bytes |
required |
public_key
|
RSAPublicKey | str | bytes
|
Public key to verify, can be an RSAPublicKey object, a PEM string, or bytes |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the key pair is valid, False otherwise |
Source code in helpers/rsa.py
verify_signature(data, signature, public_key)
staticmethod
Verifies the given signature against the given data and public key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data that was signed |
required |
signature
|
bytes
|
Signature to verify |
required |
public_key
|
RSAPublicKey | str | bytes
|
Public key to use for verification, can be an RSAPublicKey object, a PEM string, or bytes |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the signature is valid, False otherwise |
Source code in helpers/rsa.py
helpers.utils
helpers.utils
hash_bytes(data, algorithm='sha256', return_type=uuid.UUID)
Calculate the hash of the given bytes using the specified algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The data to be hashed. |
required |
algorithm
|
str
|
Hashing algorithm to use (default is 'sha256'). Options include 'md5', 'sha1', 'sha256', 'sha512', etc. |
'sha256'
|
return_type
|
type[str | UUID]
|
Type to return the hash as (default is uuid.UUID). |
UUID
|
Returns:
| Type | Description |
|---|---|
StrUuid
|
str | uuid.UUID: The hash of the data as a string or UUID. |
Source code in helpers/utils.py
hash_file(file, algorithm='sha256', return_type=uuid.UUID)
Calculate the hash of a file or its contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | Path | bytes
|
Path to the file or contents of the file as bytes. |
required |
algorithm
|
str
|
Hashing algorithm (default is 'sha256'). Options include 'md5', 'sha1', 'sha256', 'sha512', etc. |
'sha256'
|
return_type
|
type[str | UUID]
|
Type to return the hash as (default is uuid.UUID). |
UUID
|
Returns:
| Type | Description |
|---|---|
StrUuid
|
str | uuid.UUID: The hash of the file or its contents as a string or UUID. |
Source code in helpers/utils.py
hash_text(text, base_uuid=None)
Generates a hash-based UUID using the given text and an optional base UUID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to be used for generating the hash-based UUID. |
required |
base_uuid
|
UUID | None
|
The optional base UUID to use for hashing. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
|
uuid.UUID: The hash-based UUID generated from the input text and base UUID. |
Source code in helpers/utils.py
slugify(text, replace_specials_with='_', replace_spaces_with='-')
Convert a given string into a slug format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The string to be converted into a slug. |
required |
replace_specials_with
|
str
|
The character to replace special characters with. Defaults to "_. |
'_'
|
replace_spaces_with
|
str
|
The character to replace spaces with. Defaults to "-". |
'-'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The slugified string. |