Show More
@@ -1,86 +1,96 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Password generation for the IPython notebook. |
|
2 | Password generation for the IPython notebook. | |
3 | """ |
|
3 | """ | |
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 | |
16 | the generated string. |
|
17 | the generated string. | |
17 |
|
18 | |||
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`). | |
25 |
|
27 | |||
26 | Returns |
|
28 | Returns | |
27 | ------- |
|
29 | ------- | |
28 | hashed_passphrase : str |
|
30 | hashed_passphrase : str | |
29 | Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'. |
|
31 | Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'. | |
30 |
|
32 | |||
31 | Examples |
|
33 | Examples | |
32 | -------- |
|
34 | -------- | |
33 | In [1]: passwd('mypassword') |
|
35 | In [1]: passwd('mypassword') | |
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) | |
40 |
|
50 | |||
41 | return ':'.join((algorithm, salt, h.hexdigest())) |
|
51 | return ':'.join((algorithm, salt, h.hexdigest())) | |
42 |
|
52 | |||
43 | def passwd_check(hashed_passphrase, passphrase): |
|
53 | def passwd_check(hashed_passphrase, passphrase): | |
44 | """Verify that a given passphrase matches its hashed version. |
|
54 | """Verify that a given passphrase matches its hashed version. | |
45 |
|
55 | |||
46 | Parameters |
|
56 | Parameters | |
47 | ---------- |
|
57 | ---------- | |
48 | hashed_passphrase : str |
|
58 | hashed_passphrase : str | |
49 | Hashed password, in the format returned by `passwd`. |
|
59 | Hashed password, in the format returned by `passwd`. | |
50 | passphrase : str |
|
60 | passphrase : str | |
51 | Passphrase to validate. |
|
61 | Passphrase to validate. | |
52 |
|
62 | |||
53 | Returns |
|
63 | Returns | |
54 | ------- |
|
64 | ------- | |
55 | valid : bool |
|
65 | valid : bool | |
56 | True if the passphrase matches the hash. |
|
66 | True if the passphrase matches the hash. | |
57 |
|
67 | |||
58 | Examples |
|
68 | Examples | |
59 | -------- |
|
69 | -------- | |
60 | In [1]: from IPython.lib.security import passwd_check |
|
70 | In [1]: from IPython.lib.security import passwd_check | |
61 |
|
71 | |||
62 | In [2]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', |
|
72 | In [2]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', | |
63 | ...: 'mypassword') |
|
73 | ...: 'mypassword') | |
64 | Out[2]: True |
|
74 | Out[2]: True | |
65 |
|
75 | |||
66 | In [3]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', |
|
76 | In [3]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', | |
67 | ...: 'anotherpassword') |
|
77 | ...: 'anotherpassword') | |
68 | Out[3]: False |
|
78 | Out[3]: False | |
69 |
|
79 | |||
70 | """ |
|
80 | """ | |
71 | try: |
|
81 | try: | |
72 | algorithm, salt, pw_digest = hashed_passphrase.split(':', 2) |
|
82 | algorithm, salt, pw_digest = hashed_passphrase.split(':', 2) | |
73 | except (ValueError, TypeError): |
|
83 | except (ValueError, TypeError): | |
74 | return False |
|
84 | return False | |
75 |
|
85 | |||
76 | try: |
|
86 | try: | |
77 | h = hashlib.new(algorithm) |
|
87 | h = hashlib.new(algorithm) | |
78 | except ValueError: |
|
88 | except ValueError: | |
79 | return False |
|
89 | return False | |
80 |
|
90 | |||
81 | if len(pw_digest) == 0 or len(salt) != salt_len: |
|
91 | if len(pw_digest) == 0 or len(salt) != salt_len: | |
82 | return False |
|
92 | return False | |
83 |
|
93 | |||
84 | h.update(passphrase + salt) |
|
94 | h.update(passphrase + salt) | |
85 |
|
95 | |||
86 | return h.hexdigest() == pw_digest |
|
96 | return h.hexdigest() == pw_digest |
General Comments 0
You need to be logged in to leave comments.
Login now