-
Notifications
You must be signed in to change notification settings - Fork 0
/
untrusted.py
executable file
·69 lines (53 loc) · 1.38 KB
/
untrusted.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals
import sys
import string
import hashlib
from getpass import getpass
__all__ = ['untrusted']
ALPHABET = string.digits + string.ascii_letters
def clip_copy(text):
import pyperclip
pyperclip.copy(text)
def i2text(value, width=1, alphabet=ALPHABET):
n = len(alphabet)
result = []
while value != 0:
c = value % n
result.append(alphabet[c])
value //= n
np = width - len(result)
if np <= 0:
padding = []
else:
padding = [alphabet[0]] * np
sresult = ''.join(padding + result)
return sresult
def fingerprint(text):
m = hashlib.sha512()
m.update(text.encode('utf-8'))
digest = m.hexdigest()
m = hashlib.md5()
m.update(digest.encode('utf-8'))
m.update(text.encode('utf-8'))
return m.hexdigest()
def untrusted(text, size=20):
fp = fingerprint(text)
ival = int(fp, 16)
return i2text(ival)[:20]
def main(size=20):
if len(sys.argv) >= 2:
size = int(sys.argv[1])
original = getpass('text?: ')
pw = untrusted(original, size=size)
print(pw)
try:
clip_copy(pw)
except Exception:
print('Could not copy to clipboard')
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass