sentry.hashers – sentry.hashers module reference

Hashers are responsible for generating hashes of cleartext passwords, as well as validating if a given password matches a stored hash.

Password hashes

class sentry.hashers.BaseHasher
Bases: object
class sentry.hashers.HMACHasher(type_, key, binary=False)

Bases: sentry.hashers.BaseHasher A hasher that uses the HMAC algorithm to hash the plaintext.

type can be one of md5, sha1

Set binary to True if the binary digest should be returned. The default is to return the hex digest.

key is the secret value used in HMAC algorithm

>>> h = HMACHasher("md5", "key")
>>> h('04130747afca4d79e32e87cf2104f087', "hello")
True
hsh(clear)
>>> h = HMACHasher("md5", "key")
>>> h.hsh("hello")
'04130747afca4d79e32e87cf2104f087'
class sentry.hashers.HMACSaltedHasher(digestmod='sha1')

Bases: sentry.hashers.BaseHasher A hasher that uses the HMAC algorithm to hash the plaintext.

The key is stored as salt in the password hash, similar to SaltedHasher

>>> h = HMACSaltedHasher('sha1')
>>> h("NaCl:7860909fd96b106700d368036005d8484080c7f0", "hello")
True
hsh(clear, salt)
>>> h = HMACSaltedHasher('sha1')
>>> h.hsh("hello", "NaCl")
'NaCl:7860909fd96b106700d368036005d8484080c7f0'
sentry.hashers.Hasher(name, *args, **kw)

A convenience function that will return a Hasher instance given its name.

>>> h = Hasher('md5')
>>> isinstance(h, MD5Hasher)
True
class sentry.hashers.MD5Hasher(encoding='hex')

Bases: sentry.hashers.BaseHasher A hasher that does an MD5 hash of the cleartext.

Set encoding to “hex”, “base64”, or “binary” The default is “hex”.

>>> h = MD5Hasher()
>>> h('5d41402abc4b2a76b9719d911017c592', "hello")
True
>>> h('7d793037a0760186574b0282f2f435e7', "hello")
False
hsh(clear)
>>> h = MD5Hasher()
>>> h.hsh("hello")
'5d41402abc4b2a76b9719d911017c592'
>>> h = MD5Hasher(encoding = "binary")
>>> h.hsh("hello")
']A@*\xbcK*v\xb9q\x9d\x91\x10\x17\xc5\x92'
>>> h = MD5Hasher(encoding = "base64")
>>> h.hsh("hello")
'XUFAKrxLKna5cZ2REBfFkg=='
class sentry.hashers.PlainHasher

Bases: sentry.hashers.BaseHasher A no-op that just returns the cleartext.

>>> h = PlainHasher()
>>> h("hello", "hello")
True
>>> h("hello", "world")
False
hsh(clear)
>>> h = PlainHasher()
>>> h.hsh("hello")
'hello'
class sentry.hashers.SHA1Hasher(binary=False)

Bases: sentry.hashers.BaseHasher A hasher that does a SHA-1 hash of the cleartext.

Set binary to True if the binary digest should be returned. The default is to return the hex digest.

>>> h = SHA1Hasher()
>>> h('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', "hello")
True
>>> h('7c211433f02071597741e6ff5a8ea34789abbf43', "hello")
False
hsh(clear)
>>> h = SHA1Hasher()
>>> h.hsh("hello")
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
>>> h = SHA1Hasher(binary = True)
>>> h.hsh("hello")
'\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM'
class sentry.hashers.SaltedHasher(subhash)

Bases: sentry.hashers.BaseHasher A hasher that adds a salt to the output of another hash. The salt is separated from the hash by a delimeter, which defaults to ‘:’

subhash is an object that supports a hsh(plaintext) method which returns the hash of plaintext.

>>> h = SaltedHasher(MD5Hasher())
>>> h("NaCl:8127559dec9e79aa80982f935d5f8e33", "hello")
True
>>> h("NaCl:989af88dbfa386ee5f55dc5627dac6c9", "hello")
False
hsh(clear, salt)
>>> h = SaltedHasher(MD5Hasher())
>>> h.hsh("hello", "NaCl")
'NaCl:8127559dec9e79aa80982f935d5f8e33'