backend.crypt

The crypt package provides cryptographic utilities that secure authentication workflows and user data.

It centralizes password hashing, password validation, and verification code generation in order to enforce consistent security practices across the application.

Contents

  • encrypt_decrypt
    Utility module exposing the EncryptionDec class:
    • hash_password — securely hashes plaintext passwords using bcrypt

    • check_passwords — verifies a plaintext password against a hashed one

    • is_valid_password — validates password complexity rules:
      • minimum length (8+)

      • must include lowercase, uppercase, digit, and special character

    • generate_verification_code — produces numeric verification codes (default length: 6 digits) for email verification or 2FA workflows

Submodules

Classes

EncryptionDec

Utility class for password hashing, validation, and verification code generation.

Package Contents

class EncryptionDec[source]

Utility class for password hashing, validation, and verification code generation.

hash_password(text: str) str[source]

Hashes a plaintext password using bcrypt with a generated salt.

check_passwords(plain_text: str, passwd: str) bool[source]

Verifies a plaintext password against a hashed password.

is_valid_password(password: str) bool[source]

Validates that a password meets security requirements: - At least 8 characters - At least one lowercase letter - At least one uppercase letter - At least one digit - At least one special character

generate_verification_code(length: int = 6) str[source]

Generates a numeric verification code of given length (default: 6 digits).

hash_password(text: str) str[source]

Hash a plaintext password using bcrypt.

Parameters:

text (str) -- The plaintext password.

Returns:

The bcrypt-hashed password (UTF-8 decoded).

Return type:

str

check_passwords(plain_text: str, passwd: str) bool[source]

Verify if a plaintext password matches a hashed password.

Parameters:
  • plain_text (str) -- The plaintext password to check.

  • passwd (str) -- The previously hashed password to verify against.

Returns:

True if the password matches, False otherwise.

Return type:

bool

is_valid_password(password: str) bool[source]

Validate that a password meets security complexity rules.

Parameters:

password (str) -- The plaintext password to validate.

Returns:

True if password is valid, False otherwise.

Return type:

bool

Notes

  • Minimum length: 8 characters

  • Must contain at least: - one lowercase letter - one uppercase letter - one digit - one special character (!@#$%^&*(),.?":{}|<>)

generate_verification_code(length: int = 6) str[source]

Generate a numeric verification code of specified length.

Parameters:

length (int, optional) -- Number of digits in the code. Default is 6.

Returns:

A randomly generated numeric code of given length.

Return type:

str

Example

>>> enc = EncryptionDec()
>>> enc.generate_verification_code()
'493027'