Generate hashed password and salt for use in notebook configuration. In the notebook configuration, set `c.NotebookApp.password` to the generated string. Parameters ---------- passphrase : str Password to hash. If unspecified, the user is asked to input and ver
(passphrase=None, algorithm='sha1')
| 26 | #----------------------------------------------------------------------------- |
| 27 | |
| 28 | def passwd(passphrase=None, algorithm='sha1'): |
| 29 | """Generate hashed password and salt for use in notebook configuration. |
| 30 | |
| 31 | In the notebook configuration, set `c.NotebookApp.password` to |
| 32 | the generated string. |
| 33 | |
| 34 | Parameters |
| 35 | ---------- |
| 36 | passphrase : str |
| 37 | Password to hash. If unspecified, the user is asked to input |
| 38 | and verify a password. |
| 39 | algorithm : str |
| 40 | Hashing algorithm to use (e.g, 'sha1' or any argument supported |
| 41 | by :func:`hashlib.new`). |
| 42 | |
| 43 | Returns |
| 44 | ------- |
| 45 | hashed_passphrase : str |
| 46 | Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'. |
| 47 | |
| 48 | Examples |
| 49 | -------- |
| 50 | >>> passwd('mypassword') |
| 51 | 'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12' |
| 52 | |
| 53 | """ |
| 54 | if passphrase is None: |
| 55 | for i in range(3): |
| 56 | p0 = getpass.getpass('Enter password: ') |
| 57 | p1 = getpass.getpass('Verify password: ') |
| 58 | if p0 == p1: |
| 59 | passphrase = p0 |
| 60 | break |
| 61 | else: |
| 62 | print('Passwords do not match.') |
| 63 | else: |
| 64 | raise UsageError('No matching passwords found. Giving up.') |
| 65 | |
| 66 | h = hashlib.new(algorithm) |
| 67 | salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len) |
| 68 | h.update(encode(passphrase, 'utf-8') + encode(salt, 'ascii')) |
| 69 | |
| 70 | return ':'.join((algorithm, salt, h.hexdigest())) |
| 71 | |
| 72 | |
| 73 | def passwd_check(hashed_passphrase, passphrase): |