diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2020-10-05 21:14:41 -0400 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2020-10-05 21:14:41 -0400 |
| commit | 3f1ad211d9499f5c36e9cc90c888c6e0da50ab3c (patch) | |
| tree | c01df999752b89a3c272c4c0e6acff52b2977ef6 /passlib/handlers/ldap_digests.py | |
| parent | a2809fd98ed58eb006d204b05cc1519bdf1b12de (diff) | |
| download | passlib-3f1ad211d9499f5c36e9cc90c888c6e0da50ab3c.tar.gz | |
passlib.hash: added ldap_salted_sha256 & ldap_salted_512 (issue 124)
Diffstat (limited to 'passlib/handlers/ldap_digests.py')
| -rw-r--r-- | passlib/handlers/ldap_digests.py | 91 |
1 files changed, 89 insertions, 2 deletions
diff --git a/passlib/handlers/ldap_digests.py b/passlib/handlers/ldap_digests.py index 17ab774..30254f0 100644 --- a/passlib/handlers/ldap_digests.py +++ b/passlib/handlers/ldap_digests.py @@ -5,7 +5,7 @@ #============================================================================= # core from base64 import b64encode, b64decode -from hashlib import md5, sha1 +from hashlib import md5, sha1, sha256, sha512 import logging; log = logging.getLogger(__name__) import re # site @@ -22,6 +22,8 @@ __all__ = [ "ldap_sha1", "ldap_salted_md5", "ldap_salted_sha1", + "ldap_salted_sha256", + "ldap_salted_sha512", ##"get_active_ldap_crypt_schemes", "ldap_des_crypt", @@ -160,7 +162,9 @@ class ldap_salted_md5(_SaltedBase64DigestHelper): _hash_regex = re.compile(u(r"^\{SMD5\}(?P<tmp>[+/a-zA-Z0-9]{27,}={0,2})$")) class ldap_salted_sha1(_SaltedBase64DigestHelper): - """This class stores passwords using LDAP's salted SHA1 format, and follows the :ref:`password-hash-api`. + """ + This class stores passwords using LDAP's "Salted SHA1" format, + and follows the :ref:`password-hash-api`. It supports a 4-16 byte salt. @@ -196,8 +200,91 @@ class ldap_salted_sha1(_SaltedBase64DigestHelper): ident = u("{SSHA}") checksum_size = 20 _hash_func = sha1 + # NOTE: 32 = ceil((20 + 4) * 4/3) _hash_regex = re.compile(u(r"^\{SSHA\}(?P<tmp>[+/a-zA-Z0-9]{32,}={0,2})$")) + + +class ldap_salted_sha256(_SaltedBase64DigestHelper): + """ + This class stores passwords using LDAP's "Salted SHA2-256" format, + and follows the :ref:`password-hash-api`. + + It supports a 4-16 byte salt. + + The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords: + + :type salt: bytes + :param salt: + Optional salt string. + If not specified, one will be autogenerated (this is recommended). + If specified, it may be any 4-16 byte string. + + :type salt_size: int + :param salt_size: + Optional number of bytes to use when autogenerating new salts. + Defaults to 8 bytes for compatibility with the LDAP spec, + but Passlib supports any value between 4-16. + + :type relaxed: bool + :param relaxed: + By default, providing an invalid value for one of the other + keywords will result in a :exc:`ValueError`. If ``relaxed=True``, + and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning` + will be issued instead. Correctable errors include + ``salt`` strings that are too long. + + .. versionadded:: 1.7.3 + """ + name = "ldap_salted_sha256" + ident = u("{SSHA256}") + checksum_size = 32 + default_salt_size = 8 + _hash_func = sha256 + # NOTE: 48 = ceil((32 + 4) * 4/3) + _hash_regex = re.compile(u(r"^\{SSHA256\}(?P<tmp>[+/a-zA-Z0-9]{48,}={0,2})$")) + + +class ldap_salted_sha512(_SaltedBase64DigestHelper): + """ + This class stores passwords using LDAP's "Salted SHA2-512" format, + and follows the :ref:`password-hash-api`. + + It supports a 4-16 byte salt. + + The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords: + + :type salt: bytes + :param salt: + Optional salt string. + If not specified, one will be autogenerated (this is recommended). + If specified, it may be any 4-16 byte string. + + :type salt_size: int + :param salt_size: + Optional number of bytes to use when autogenerating new salts. + Defaults to 8 bytes for compatibility with the LDAP spec, + but Passlib supports any value between 4-16. + + :type relaxed: bool + :param relaxed: + By default, providing an invalid value for one of the other + keywords will result in a :exc:`ValueError`. If ``relaxed=True``, + and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning` + will be issued instead. Correctable errors include + ``salt`` strings that are too long. + + .. versionadded:: 1.7.3 + """ + name = "ldap_salted_sha512" + ident = u("{SSHA512}") + checksum_size = 64 + default_salt_size = 8 + _hash_func = sha512 + # NOTE: 91 = ceil((64 + 4) * 4/3) + _hash_regex = re.compile(u(r"^\{SSHA512\}(?P<tmp>[+/a-zA-Z0-9]{91,}={0,2})$")) + + class ldap_plaintext(plaintext): """This class stores passwords in plaintext, and follows the :ref:`password-hash-api`. |
