##// END OF EJS Templates
Allow any hashing algorithm.
Stefan van der Walt -
Show More
@@ -5,7 +5,7 b' Password generation for the IPython notebook.'
5 import hashlib
5 import hashlib
6 import random
6 import random
7
7
8 def passwd(passphrase):
8 def passwd(passphrase, algorithm='sha1'):
9 """Generate hashed password and salt for use in notebook configuration.
9 """Generate hashed password and salt for use in notebook configuration.
10
10
11 In the notebook configuration, set `c.NotebookApp.password` to
11 In the notebook configuration, set `c.NotebookApp.password` to
@@ -15,6 +15,8 b' def passwd(passphrase):'
15 ----------
15 ----------
16 passphrase : str
16 passphrase : str
17 Password to hash.
17 Password to hash.
18 algorithm : str
19 Hashing algorithm to use.
18
20
19 Returns
21 Returns
20 -------
22 -------
@@ -27,8 +29,6 b' def passwd(passphrase):'
27 Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'
29 Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'
28
30
29 """
31 """
30 algorithm = 'sha1'
31
32 h = hashlib.new(algorithm)
32 h = hashlib.new(algorithm)
33 salt = hex(int(random.getrandbits(16)))[2:]
33 salt = hex(int(random.getrandbits(16)))[2:]
34 h.update(passphrase + salt)
34 h.update(passphrase + salt)
@@ -63,20 +63,19 b' def passwd_check(hashed_passphrase, passphrase):'
63 Out[3]: False
63 Out[3]: False
64
64
65 """
65 """
66 # Algorithm and hash length
67 supported_algorithms = {'sha1': 40}
68
69 try:
66 try:
70 algorithm, salt, pw_digest = hashed_passphrase.split(':', 2)
67 algorithm, salt, pw_digest = hashed_passphrase.split(':', 2)
71 except (ValueError, TypeError):
68 except (ValueError, TypeError):
72 return False
69 return False
73
70
74 if not (algorithm in supported_algorithms and \
71 try:
75 len(pw_digest) == supported_algorithms[algorithm] and \
72 h = hashlib.new(algorithm)
76 len(salt) == 4):
73 except ValueError:
74 return False
75
76 if len(pw_digest) == 0 or len(salt) != 4:
77 return False
77 return False
78
78
79 h = hashlib.new(algorithm)
80 h.update(passphrase + salt)
79 h.update(passphrase + salt)
81
80
82 return h.hexdigest() == pw_digest
81 return h.hexdigest() == pw_digest
General Comments 0
You need to be logged in to leave comments. Login now