Added a saltHash function.

This commit is contained in:
Jeremy Fincher 2003-10-28 14:20:00 +00:00
parent 80deb23897
commit b63f2170e8
2 changed files with 15 additions and 0 deletions

View File

@ -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.
"""

View File

@ -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)