Show More
@@ -4,12 +4,13 b' Password generation for the IPython notebook.' | |||||
4 |
|
4 | |||
5 | import hashlib |
|
5 | import hashlib | |
6 | import random |
|
6 | import random | |
|
7 | import getpass | |||
7 |
|
8 | |||
8 | # Length of the salt in nr of hex chars, which implies salt_len * 4 |
|
9 | # Length of the salt in nr of hex chars, which implies salt_len * 4 | |
9 | # bits of randomness. |
|
10 | # bits of randomness. | |
10 | salt_len = 12 |
|
11 | salt_len = 12 | |
11 |
|
12 | |||
12 | def passwd(passphrase, algorithm='sha1'): |
|
13 | def passwd(passphrase='', algorithm='sha1'): | |
13 | """Generate hashed password and salt for use in notebook configuration. |
|
14 | """Generate hashed password and salt for use in notebook configuration. | |
14 |
|
15 | |||
15 | In the notebook configuration, set `c.NotebookApp.password` to |
|
16 | In the notebook configuration, set `c.NotebookApp.password` to | |
@@ -18,7 +19,8 b" def passwd(passphrase, algorithm='sha1'):" | |||||
18 | Parameters |
|
19 | Parameters | |
19 | ---------- |
|
20 | ---------- | |
20 | passphrase : str |
|
21 | passphrase : str | |
21 | Password to hash. |
|
22 | Password to hash. If unspecified, the user is asked to input | |
|
23 | and verify a password. | |||
22 | algorithm : str |
|
24 | algorithm : str | |
23 | Hashing algorithm to use (e.g, 'sha1' or any argument supported |
|
25 | Hashing algorithm to use (e.g, 'sha1' or any argument supported | |
24 | by :func:`hashlib.new`). |
|
26 | by :func:`hashlib.new`). | |
@@ -34,6 +36,14 b" def passwd(passphrase, algorithm='sha1'):" | |||||
34 | Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12' |
|
36 | Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12' | |
35 |
|
37 | |||
36 | """ |
|
38 | """ | |
|
39 | if not passphrase: | |||
|
40 | p0 = getpass.getpass('Enter password: ') | |||
|
41 | p1 = getpass.getpass('Verify password: ') | |||
|
42 | if (p0 == p1): | |||
|
43 | passphrase = p0 | |||
|
44 | else: | |||
|
45 | raise ValueError('Passwords did not match.') | |||
|
46 | ||||
37 | h = hashlib.new(algorithm) |
|
47 | h = hashlib.new(algorithm) | |
38 | salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len) |
|
48 | salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len) | |
39 | h.update(passphrase + salt) |
|
49 | h.update(passphrase + salt) |
General Comments 0
You need to be logged in to leave comments.
Login now