diff options
Diffstat (limited to 'passlib/handler.py')
| -rw-r--r-- | passlib/handler.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/passlib/handler.py b/passlib/handler.py index 33fbbc1..f6b4710 100644 --- a/passlib/handler.py +++ b/passlib/handler.py @@ -12,7 +12,7 @@ import time import os #site #libs -from passlib.utils import abstract_class_method, classproperty +from passlib.utils import abstract_class_method, classproperty, H64_CHARS, generate_h64_salt #pkg #local __all__ = [ @@ -322,6 +322,8 @@ class CryptHandler(object): class CryptHandlerHelper(CryptHandler): "class providing some helpful methods for implementing a crypt algorithm" + salt_chars = None #fill in with minimum number of salt chars required, and _norm_salt() will handle truncating etc + @classproperty def setting_kwds(cls): "auto-calculates setting_kwds for the 3 most common cases, autodetecting via other informational attributes" @@ -351,6 +353,23 @@ class CryptHandlerHelper(CryptHandler): rounds = mn return rounds + @classmethod + def _norm_salt(cls, salt): + count = cls.salt_chars + assert count, "cls.salt_chars not set" + if not salt: + return generate_h64_salt(count) + for c in salt: + if c not in H64_CHARS: + raise ValueError, "invalid character in %s salt: %r" % (cls.name, c) + if len(salt) < count: + raise ValueError, "%s salt must be at least %d chars" % (cls.name, count) + elif len(salt) > count: + #automatically clip things to specified number of chars + return salt[:count] + else: + return salt + #========================================================= # eof #========================================================= |
