Skip to main content

PBKDF2PasswordHasher

Secure password hashing using the PBKDF2 algorithm (recommended)

Attributes

AttributeTypeDescription
algorithmstring = "pbkdf2_sha256"The identifier for the hashing algorithm used to label and verify encoded password strings.
iterationsinteger = 1500000The number of PBKDF2 hashing rounds applied to the password to increase computational cost and security.
digestcallable = hashlib.sha256The HMAC digest algorithm used by the PBKDF2 function, defaulting to SHA256.

Constructor

Signature

def PBKDF2PasswordHasher()

Methods


encode()

@classmethod
def encode(
password: string,
salt: string,
iterations: integer = None
) - > string

Generates a secure PBKDF2 hash string from a raw password and salt, incorporating the algorithm name and iteration count for storage.

Parameters

NameTypeDescription
passwordstringThe raw password string to be hashed.
saltstringA unique seed string used to ensure the resulting hash is unique even if passwords are identical.
iterationsinteger = NoneThe number of PBKDF2 rounds to perform; defaults to the class-defined iteration count if not provided.

Returns

TypeDescription
stringA formatted string containing the algorithm, iteration count, salt, and base64-encoded hash separated by dollar signs.

decode()

@classmethod
def decode(
encoded: string
) - > object

Parses an encoded password string into its constituent components for verification or analysis.

Parameters

NameTypeDescription
encodedstringThe formatted hash string retrieved from storage.

Returns

TypeDescription
objectA dictionary containing the algorithm name, hash value, iteration count, and salt extracted from the encoded string.

verify()

@classmethod
def verify(
password: string,
encoded: string
) - > boolean

Validates a raw password against an existing encoded hash using a constant-time comparison to prevent timing attacks.

Parameters

NameTypeDescription
passwordstringThe raw password provided by the user for authentication.
encodedstringThe previously stored hash string to validate against.

Returns

TypeDescription
booleanTrue if the password generates a hash matching the encoded string, False otherwise.

safe_summary()

@classmethod
def safe_summary(
encoded: string
) - > object

Provides a human-readable summary of the hash components with sensitive data like the salt and hash masked for safe logging or display.

Parameters

NameTypeDescription
encodedstringThe encoded hash string to summarize.

Returns

TypeDescription
objectA dictionary of descriptive labels and masked values representing the hash metadata.

must_update()

@classmethod
def must_update(
encoded: string
) - > boolean

Determines if an encoded hash should be re-hashed to meet current security standards, such as increased iterations or salt entropy requirements.

Parameters

NameTypeDescription
encodedstringThe encoded hash string currently stored in the database.

Returns

TypeDescription
booleanTrue if the hash's iterations or salt do not match current hasher settings, indicating an update is needed.

harden_runtime()

@classmethod
def harden_runtime(
password: string,
encoded: string
)

Performs additional hashing iterations at runtime to bridge the gap between an older hash's iteration count and the current system standard, mitigating timing differences.

Parameters

NameTypeDescription
passwordstringThe raw password used to perform the additional hashing rounds.
encodedstringThe existing encoded hash used to determine how many extra iterations are required.