forked from vladiuz1/py3-jupyter-docker-compose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassphrase
executable file
·115 lines (100 loc) · 4.3 KB
/
passphrase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# _ _ _____ _
# | | | | | __ \ | |
# | |_ _ _ __ _ _| |_ ___ _ __ | |__) |_ _ ___ ___ _ __ | |__ _ __ __ _ ___ ___
# _ | | | | | '_ \| | | | __/ _ \ '__| | ___/ _` / __/ __| '_ \| '_ \| '__/ _` / __|/ _ \
# | |__| | |_| | |_) | |_| | || __/ | | | | (_| \__ \__ \ |_) | | | | | | (_| \__ \ __/
# \____/ \__,_| .__/ \__, |\__\___|_| |_| \__,_|___/___/ .__/|_| |_|_| \__,_|___/\___|
# / ____| | | __/ | | | | |
# | | __ ___ |_|_ |___/ __ __ _| |_ ___ _ __ |_|
# | | |_ |/ _ \ '_ \ / _ \ '__/ _` | __/ _ \| '__|
# | |__| | __/ | | | __/ | | (_| | || (_) | |
# \_____|\___|_| |_|\___|_| \__,_|\__\___/|_|
#
# run ./passphrase
# to generate a passphrase to access your jupyter-notebook
#
#
# To remove dependancies on non-standard
# modules code snippets taken from:
# https://github.com/ipython/ipython_genutils/blob/master/ipython_genutils/encoding.py
# - Copyrgiht Copyright (C) 2008-2012 The IPython Development Team
#
# https://github.com/jupyter/notebook/blob/master/notebook/auth/security.py
# - Copyright (c) 2001-2015, IPython Development Team
# - Copyright (c) 2015-, Jupyter Development Team
#
import hashlib
import getpass
import random
import sys
import locale
import warnings
import platform
salt_len = 12
algorithm = 'sha1'
# to deal with the possibility of sys.std* not being a stream at all
def get_stream_enc(stream, default=None):
"""Return the given stream's encoding or a default.
There are cases where ``sys.std*`` might not actually be a stream, so
check for the encoding attribute prior to returning it, and return
a default if it doesn't exist or evaluates as False. ``default``
is None if not provided.
"""
if not hasattr(stream, 'encoding') or not stream.encoding:
return default
else:
return stream.encoding
# Less conservative replacement for sys.getdefaultencoding, that will try
# to match the environment.
# Defined here as central function, so if we find better choices, we
# won't need to make changes all over IPython.
def getdefaultencoding(prefer_stream=True):
"""Return IPython's guess for the default encoding for bytes as text.
If prefer_stream is True (default), asks for stdin.encoding first,
to match the calling Terminal, but that is often None for subprocesses.
Then fall back on locale.getpreferredencoding(),
which should be a sensible platform default (that respects LANG environment),
and finally to sys.getdefaultencoding() which is the most conservative option,
and usually ASCII on Python 2 or UTF8 on Python 3.
"""
enc = None
if prefer_stream:
enc = get_stream_enc(sys.stdin)
if not enc or enc == 'ascii':
try:
# There are reports of getpreferredencoding raising errors
# in some cases, which may well be fixed, but let's be conservative here.
enc = locale.getpreferredencoding()
except Exception:
pass
enc = enc or sys.getdefaultencoding()
# On windows `cp0` can be returned to indicate that there is no code page.
# Since cp0 is an invalid encoding return instead cp1252 which is the
# Western European default.
if enc == 'cp0':
warnings.warn(
"Invalid code page cp0 detected - using cp1252 instead."
"If cp1252 is incorrect please ensure a valid code page "
"is defined for the process.", RuntimeWarning)
return 'cp1252'
return enc
DEFAULT_ENCODING = getdefaultencoding()
def no_code(x, encoding=None):
return x
def encode(u, encoding=None):
encoding = encoding or DEFAULT_ENCODING
return u.encode(encoding, "replace")
if sys.version_info[0] >= 3 or platform.python_implementation() == 'IronPython':
str_to_bytes = encode
else:
str_to_bytes = no_code
def cast_bytes(s, encoding=None):
if not isinstance(s, bytes):
return encode(s, encoding)
return s
passphrase = getpass.getpass('Passphrase: ')
h = hashlib.new(algorithm)
salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))
print(':'.join((algorithm, salt, h.hexdigest())))