source: main/waeup.kofa/trunk/tools/make_ssha.py @ 17949

Last change on this file since 17949 was 14635, checked in by Henrik Bettermann, 8 years ago

Add tool to generate SSHA password hash.

File size: 861 bytes
Line 
1import hashlib
2import os
3import sys
4from base64 import urlsafe_b64encode as encode
5from base64 import urlsafe_b64decode as decode
6
7def makeSecret(password):
8    salt = os.urandom(4)
9    h = hashlib.sha1(password)
10    h.update(salt)
11    return "{SSHA}" + encode(h.digest() + salt)
12
13def checkPassword(password, challenge_password):
14    challenge_bytes = decode(challenge_password[6:])
15    digest = challenge_bytes[:20]
16    salt = challenge_bytes[20:]
17    hr = hashlib.sha1(password)
18    hr.update(salt)
19    return digest == hr.digest()
20
21if __name__=='__main__':
22    if len(sys.argv) < 3:
23        print 'Usage: %s <encode or check> <password> <ssha string>' % __file__
24        sys.exit(1)
25    if sys.argv[1] == 'encode':
26        print makeSecret('%s' % sys.argv[2])
27    if sys.argv[1] == 'check':
28        print checkPassword('%s' % sys.argv[2], '%s' % sys.argv[3])
Note: See TracBrowser for help on using the repository browser.