diff --git a/src/utils.py b/src/utils.py index a34c9a009..bf87ce4d0 100755 --- a/src/utils.py +++ b/src/utils.py @@ -39,6 +39,8 @@ import fix import os import re +import md5 +import sha import string import sgmllib import textwrap @@ -403,6 +405,15 @@ def flatten(seq, strings=False): except TypeError: yield elt +def saltHash(password, salt=None, hash='sha'): + if salt is None: + salt = mktemp()[:8] + if hash == 'md5': + hasher = md5.md5 + elif hash == 'sha': + hasher = sha.sha + return salt + hasher(salt + password).hexdigest() + class IterableMap(object): """Define .iteritems() in a class and subclass this to get the other iters. """ diff --git a/test/test_utils.py b/test/test_utils.py index cbd28ab2f..4d3ecc417 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -275,6 +275,10 @@ class UtilsTest(unittest.TestCase): self.failUnless(len(f('x'*35, 30)) <= 30) self.failUnless(f(' '.join(['xxxx']*10), 30)[:-3].endswith('xxxx')) + def testSaltHash(self): + s = utils.saltHash('jemfinch') + self.assertEqual(utils.saltHash('jemfinch', salt=s[:8]), s) +