Show More
@@ -1,79 +1,82 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 |
|
7 | |||
8 | def passwd(passphrase): |
|
8 | def passwd(passphrase): | |
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 | |||
|
12 | the generated string. | |||
|
13 | ||||
11 | Parameters |
|
14 | Parameters | |
12 | ---------- |
|
15 | ---------- | |
13 | passphrase : str |
|
16 | passphrase : str | |
14 | Password to hash. |
|
17 | Password to hash. | |
15 |
|
18 | |||
16 | Returns |
|
19 | Returns | |
17 | ------- |
|
20 | ------- | |
18 | hashed_passphrase : str |
|
21 | hashed_passphrase : str | |
19 | Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'. |
|
22 | Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'. | |
20 |
|
23 | |||
21 | Examples |
|
24 | Examples | |
22 | -------- |
|
25 | -------- | |
23 | In [1]: passwd('mypassword') |
|
26 | In [1]: passwd('mypassword') | |
24 | Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12' |
|
27 | Out[1]: 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12' | |
25 |
|
28 | |||
26 | """ |
|
29 | """ | |
27 | algorithm = 'sha1' |
|
30 | algorithm = 'sha1' | |
28 |
|
31 | |||
29 | h = hashlib.new(algorithm) |
|
32 | h = hashlib.new(algorithm) | |
30 | salt = hex(int(random.getrandbits(16)))[2:] |
|
33 | salt = hex(int(random.getrandbits(16)))[2:] | |
31 | h.update(passphrase + salt) |
|
34 | h.update(passphrase + salt) | |
32 |
|
35 | |||
33 | return ':'.join((algorithm, salt, h.hexdigest())) |
|
36 | return ':'.join((algorithm, salt, h.hexdigest())) | |
34 |
|
37 | |||
35 | def passwd_check(hashed_passphrase, passphrase): |
|
38 | def passwd_check(hashed_passphrase, passphrase): | |
36 | """Verify that a given passphrase matches its hashed version. |
|
39 | """Verify that a given passphrase matches its hashed version. | |
37 |
|
40 | |||
38 | Parameters |
|
41 | Parameters | |
39 | ---------- |
|
42 | ---------- | |
40 | hashed_passphrase : str |
|
43 | hashed_passphrase : str | |
41 | Hashed password, in the format returned by `passwd`. |
|
44 | Hashed password, in the format returned by `passwd`. | |
42 | passphrase : str |
|
45 | passphrase : str | |
43 | Passphrase to validate. |
|
46 | Passphrase to validate. | |
44 |
|
47 | |||
45 | Returns |
|
48 | Returns | |
46 | ------- |
|
49 | ------- | |
47 | valid : bool |
|
50 | valid : bool | |
48 | True if the passphrase matches the hash. |
|
51 | True if the passphrase matches the hash. | |
49 |
|
52 | |||
50 | Examples |
|
53 | Examples | |
51 | -------- |
|
54 | -------- | |
52 | In [1]: from IPython.lib.security import passwd_check |
|
55 | In [1]: from IPython.lib.security import passwd_check | |
53 |
|
56 | |||
54 | In [2]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', |
|
57 | In [2]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', | |
55 | ...: 'mypassword') |
|
58 | ...: 'mypassword') | |
56 | Out[2]: True |
|
59 | Out[2]: True | |
57 |
|
60 | |||
58 | In [3]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', |
|
61 | In [3]: passwd_check('sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12', | |
59 | ...: 'anotherpassword') |
|
62 | ...: 'anotherpassword') | |
60 | Out[3]: False |
|
63 | Out[3]: False | |
61 |
|
64 | |||
62 | """ |
|
65 | """ | |
63 | # Algorithm and hash length |
|
66 | # Algorithm and hash length | |
64 | supported_algorithms = {'sha1': 40} |
|
67 | supported_algorithms = {'sha1': 40} | |
65 |
|
68 | |||
66 | try: |
|
69 | try: | |
67 | algorithm, salt, pw_digest = hashed_passphrase.split(':', 2) |
|
70 | algorithm, salt, pw_digest = hashed_passphrase.split(':', 2) | |
68 | except (ValueError, TypeError): |
|
71 | except (ValueError, TypeError): | |
69 | return False |
|
72 | return False | |
70 |
|
73 | |||
71 | if not (algorithm in supported_algorithms and \ |
|
74 | if not (algorithm in supported_algorithms and \ | |
72 | len(pw_digest) == supported_algorithms[algorithm] and \ |
|
75 | len(pw_digest) == supported_algorithms[algorithm] and \ | |
73 | len(salt) == 4): |
|
76 | len(salt) == 4): | |
74 | return False |
|
77 | return False | |
75 |
|
78 | |||
76 | h = hashlib.new(algorithm) |
|
79 | h = hashlib.new(algorithm) | |
77 | h.update(passphrase + salt) |
|
80 | h.update(passphrase + salt) | |
78 |
|
81 | |||
79 | return h.hexdigest() == pw_digest |
|
82 | return h.hexdigest() == pw_digest |
General Comments 0
You need to be logged in to leave comments.
Login now