pysasl.hashing Package

Provides an abstraction and several implementations for the ability to hash and verify secrets.

class pysasl.hashing.HashT

Type variable for a HashInterface.

alias of TypeVar(‘HashT’, bound=HashInterface)

class pysasl.hashing.HashInterface(*args, **kwargs)[source]

Defines a basic interface for hash implementations. This is specifically designed to be compatible with passlib hashes.

abstract copy(**kwargs)[source]

Return a copy of the hash implementation. The kwargs may be used by some hashes to modify settings on the hash.

Parameters:
  • self (HashT) – The hash object being copied.

  • kwargs (Any) – Updated settings for the returned hash.

Return type:

HashT

abstract hash(secret)[source]

Hash the value and return the digest.

Parameters:

secret (str) – The string to hash.

Return type:

str

abstract verify(secret, hash)[source]

Check the secret against the given hash.

Parameters:
  • secret (str) – The string to check.

  • hash (str) – The hashed digest string.

Return type:

bool

class pysasl.hashing.BuiltinHash(*, hash_name='sha256', salt_len=16, rounds=500000)[source]

Implements HashInterface using the hashlib.pbkdf2_hmac() function and random salt.

The constructor arguments are the values used when encoding. When decoding, these values are read from the digest format.

Parameters:
  • hash_name (Literal['sha1', 'sha256', 'sha512']) – The hash name.

  • salt_len (int) – The length of the random salt.

  • rounds (int) – The number of hash rounds.

copy(*, hash_name=None, salt_len=None, rounds=None, **kwargs)[source]

Return a copy of the hash implementation, possibly with updated parameters.

Parameters:
  • hash_name (str | None) – The updated hash name.

  • salt_len (int | None) – The updated length of the random salt.

  • rounds (int | None) – The updated number of hash rounds.

  • kwargs (Any) – Additional keyword arguments are ignored.

Return type:

BuiltinHash

hash(secret, salt=None)[source]

Hash the secret and return the digest.

Parameters:
  • secret (str) – The string to hash.

  • salt (bytes | None) – A salt value to use instead of a random value.

Return type:

str

verify(secret, hash)[source]

Check the secret against the given hash.

Parameters:
  • secret (str) – The string to check.

  • hash (str) – The hashed digest string.

Return type:

bool

class pysasl.hashing.Cleartext(*args, **kwargs)[source]

Implements HashInterface with no hashing performed.

copy(**kwargs)[source]

Return a copy of the hash implementation. The kwargs may be used by some hashes to modify settings on the hash.

Parameters:
  • self – The hash object being copied.

  • kwargs (Any) – Updated settings for the returned hash.

Return type:

Cleartext

hash(secret)[source]

Hash the value and return the digest.

Parameters:

secret (str) – The string to hash.

Return type:

str

verify(secret, hash)[source]

Check the secret against the given hash.

Parameters:
  • secret (str) – The string to check.

  • hash (str) – The hashed digest string.

Return type:

bool