-
Notifications
You must be signed in to change notification settings - Fork 0
/
memorypalace_function.py
189 lines (154 loc) · 5.38 KB
/
memorypalace_function.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 1 17:14:28 2020
@author: alicia
"""
#key = 'WhiteBirds', answer = 'e3cfgya1w3'
import string
import random
commonwords = [line.rstrip('\n') for line in open("The_Oxford_3000.txt")]
commonwords[0] = 'a'
def memorypalace(key):
# print(key)
if len(key)%2 != 0:
# changes for every person
key+= 'r'
letter_groups = []
for i in range(0,len(key),2):
letter_groups.append(key[i:i+2])
alphabets = {}
for i in range(len(string.ascii_lowercase)):
alphabets[string.ascii_lowercase[i]] = i+1
alphabets[string.ascii_uppercase[i]] = i+1
letterstoalphabets = {}
for i in range(len(string.ascii_lowercase)):
letterstoalphabets[i+1] = string.ascii_lowercase[i]
concatenated = []
for group in letter_groups:
concatenate = alphabets[group[0]]+alphabets[group[1]]
if concatenate > 26:
concatenate = concatenate - 26
concatenated.append(letterstoalphabets[concatenate])
# whether they choose number or symbol changes with every person
symbol_to_right = {'q':'2',
'w':'3',
'r':'5',
't':'6',
'y':'7',
'p':'-',
's':'e',
'd':'r',
'f':'t',
'g':'y',
'h':'u',
'j':'i',
'k':'o',
'l':'p',
'z':'s',
'x':'d',
'c':'f',
'v':'g',
'b':'h',
'n':'j',
'm':'k'}
vowel_to_left = {'a':'1',
'e':'3',
'u':'7',
'i':'8',
'o':'9'}
password = []
for letter in concatenated:
if letter in ['a', 'e', 'i', 'o', 'u']:
encode = vowel_to_left[letter]
password.append(letter+encode)
else:
encode = symbol_to_right[letter]
password.append(letter+encode)
return ''.join(password)
def single_key(number1, number2):
key = commonwords[number1] + commonwords[number2]
for letter in key:
if letter not in string.ascii_letters:
key = key.replace(letter, '')
return key, memorypalace(key)
def collision_test(number):
keyandpass = []
allpasses = []
keys = []
i = 0
while i < number:
number1 = random.randrange(len(commonwords))
number2 = random.randrange(len(commonwords))
key, password = single_key(number1, number2)
allpasses.append(password)
keys.append(key)
keyandpass.append((key, password))
i+=1
return keys, allpasses, keyandpass
allkeys,allpasswords,keysandpasses = collision_test(50000)
import numpy
# create an array of all passwords
x=numpy.array(allpasswords)
uniquepasswords = numpy.unique(x)
# check if all keys are unique
len(allkeys) == len(numpy.unique(allkeys))
# check if all passwords are unique
# True if they are, false if not
len(x) == len(uniquepasswords)
import hashlib
def sha_hash(number1, number2):
key = commonwords[number1] + commonwords[number2]
return key, shasymbolfreq(key)
def shasymbolfreq(key):
# print(key)
h = hashlib.sha3_256()
# update hash with key, which is converted to bytes from ascii encoding
h.update(bytes(key, 'ascii'))
secret = h.digest().decode('latin-1')
return secret
def makepasswords(numberofpasswords):
keyandpass = []
allpasses = []
i = 0
while i < numberofpasswords:
number1 = random.randrange(len(commonwords))
number2 = random.randrange(len(commonwords))
key, password = sha_hash(number1, number2)
allpasses.append(password)
keyandpass.append((key, password))
i+=1
return keyandpass, allpasses
def getcharfrequencies(allpasses):
concatenatedpasswords = ''.join(allpasses)
totalnumberofcharacters = len(concatenatedpasswords)
# publicly available string of symbols extracted
printable = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~£’”€₹'
# singleapostrophechar = '\''
# find any characters entered that aren't in printable
# double check that we have all characters
notprintable = []
for symbol in concatenatedpasswords:
if (symbol not in printable) and (symbol not in notprintable):
notprintable.append(symbol)
# calculate frequency of each symbol in printable
frequencies = []
for character in printable:
freq_character = concatenatedpasswords.count(character)
frequencies.append(freq_character)
# frequencies.append(concatenatedpasswords.count(singleapostrophechar))
freq_out_of_range_characters = 0
# error handling in case a character can be printed
for character in notprintable:
# find the frequency of the character not in printable
freq_character = concatenatedpasswords.count(character)
freq_out_of_range_characters += freq_character
frequencies.append(freq_out_of_range_characters)
allsymbols = printable+ ' out of range character '
# the below line is used because it rescales frequencies, since some methods have more passwords than others
# numpy.array(frequencies)/totalnumberofcharacters * 10000
# * 10000 is used since * 100 produces very long decimals
return allsymbols, numpy.array(frequencies)/totalnumberofcharacters * 10000
# extract SHA256 hashes and treat them as passwords
keyandpass, allpasses = makepasswords(50)
allchars, frequencies = getcharfrequencies(allpasses)