V2.0
parent
0870f897bc
commit
3f6efe921b
|
@ -0,0 +1,72 @@
|
||||||
|
import pygad
|
||||||
|
import numpy
|
||||||
|
from main import hasher
|
||||||
|
hashing = hasher()
|
||||||
|
target = "dies ist ein test weisst du?"
|
||||||
|
stringlength = int(len(target)/4)
|
||||||
|
target = hashing.mhash(target)
|
||||||
|
generations = 5000
|
||||||
|
|
||||||
|
def similar(str1, str2):
|
||||||
|
str1 = str1 + ' ' * (len(str2) - len(str1))
|
||||||
|
str2 = str2 + ' ' * (len(str1) - len(str2))
|
||||||
|
return sum(1 if i == j else 0
|
||||||
|
for i, j in zip(str1, str2)) / float(len(str1))
|
||||||
|
global counter
|
||||||
|
counter = 0
|
||||||
|
def fitness_func(ga_instance, solution, solution_idx):
|
||||||
|
global counter
|
||||||
|
temp = ""
|
||||||
|
for i in solution:
|
||||||
|
temp = temp + str(i)
|
||||||
|
output = hashing.mhash(temp)
|
||||||
|
print(" ", end="\r", flush=True)
|
||||||
|
print(output+(" "*(20-len(output)))+str(counter)+"/"+str(generations), end="\r", flush=True)
|
||||||
|
counter += 1
|
||||||
|
return similar(target, output)
|
||||||
|
fitness_function = fitness_func
|
||||||
|
|
||||||
|
num_generations = generations
|
||||||
|
num_parents_mating = 4
|
||||||
|
|
||||||
|
sol_per_pop = 8
|
||||||
|
|
||||||
|
num_genes = stringlength
|
||||||
|
|
||||||
|
init_range_low = -2
|
||||||
|
init_range_high = 5
|
||||||
|
|
||||||
|
parent_selection_type = "sss"
|
||||||
|
keep_parents = 1
|
||||||
|
|
||||||
|
crossover_type = "single_point"
|
||||||
|
|
||||||
|
mutation_type = "random"
|
||||||
|
mutation_percent_genes = 10
|
||||||
|
space = []
|
||||||
|
for i in range(num_genes):
|
||||||
|
space.append(range(100))
|
||||||
|
ga_instance = pygad.GA(num_generations=num_generations,
|
||||||
|
num_parents_mating=num_parents_mating,
|
||||||
|
fitness_func=fitness_function,
|
||||||
|
sol_per_pop=sol_per_pop,
|
||||||
|
num_genes=num_genes,
|
||||||
|
init_range_low=init_range_low,
|
||||||
|
init_range_high=init_range_high,
|
||||||
|
parent_selection_type=parent_selection_type,
|
||||||
|
keep_parents=keep_parents,
|
||||||
|
crossover_type=crossover_type,
|
||||||
|
mutation_type=mutation_type,
|
||||||
|
mutation_percent_genes=mutation_percent_genes,
|
||||||
|
gene_space=space)
|
||||||
|
ga_instance.run()
|
||||||
|
solution, solution_fitness, solution_idx = ga_instance.best_solution()
|
||||||
|
print()
|
||||||
|
print("param: "+str(solution))
|
||||||
|
print("fitness: "+str(solution_fitness))
|
||||||
|
print("target: "+target)
|
||||||
|
temp = ""
|
||||||
|
for i in solution:
|
||||||
|
temp = temp + str(i)
|
||||||
|
output = hashing.mhash(temp)
|
||||||
|
print("calculated guess: "+output)
|
|
@ -0,0 +1,15 @@
|
||||||
|
import main
|
||||||
|
import string
|
||||||
|
characters = list(string.printable)
|
||||||
|
hash = input("hash? ")
|
||||||
|
hashing = main.hasher()
|
||||||
|
print("decoding into decimal...")
|
||||||
|
out = []
|
||||||
|
for i in hash:
|
||||||
|
out.append(characters.index(i))
|
||||||
|
print("decoded! "+str(out))
|
||||||
|
print("decoding to bin...")
|
||||||
|
nout = []
|
||||||
|
for i in out:
|
||||||
|
nout.append(hashing.tobin(i))
|
||||||
|
print("decoded! "+str(nout))
|
|
@ -0,0 +1,26 @@
|
||||||
|
split into 7 blocks
|
||||||
|
|
||||||
|
"dies is" "t ein t" "est."
|
||||||
|
|
||||||
|
turn into numbers
|
||||||
|
|
||||||
|
"14 19 15 29" etc.
|
||||||
|
|
||||||
|
turn into binary
|
||||||
|
|
||||||
|
"0111" etc.
|
||||||
|
|
||||||
|
turn binary into numbers
|
||||||
|
|
||||||
|
"7" etc.
|
||||||
|
|
||||||
|
turn numbers into characters
|
||||||
|
|
||||||
|
"6"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
26
py/main.py
26
py/main.py
|
@ -7,7 +7,7 @@ class hasher:
|
||||||
def todec(self, target):
|
def todec(self, target):
|
||||||
return int("0b"+target, 2)
|
return int("0b"+target, 2)
|
||||||
|
|
||||||
def toblocks(self, target, size=4):
|
def toblocks(self, target, size=7):
|
||||||
blocks = []
|
blocks = []
|
||||||
temp = ""
|
temp = ""
|
||||||
for i in range(len(target)):
|
for i in range(len(target)):
|
||||||
|
@ -23,7 +23,7 @@ class hasher:
|
||||||
def strbin(self, target):
|
def strbin(self, target):
|
||||||
temp = ""
|
temp = ""
|
||||||
for i in target:
|
for i in target:
|
||||||
temp = temp + str(characters.index(i)%2)
|
temp = temp + str(self.tobin(characters.index(i)))
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
def prep(self, target):
|
def prep(self, target):
|
||||||
|
@ -47,11 +47,25 @@ class hasher:
|
||||||
out = out + characters[i%len(characters)]
|
out = out + characters[i%len(characters)]
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
hashing = hasher()
|
hashing = hasher()
|
||||||
target = "Hello, World!"
|
target = "Hello, World! This is the hashing test string."
|
||||||
print("single character hash: "+hashing.ohash(target))
|
"""file = filedialog.askopenfilename()
|
||||||
print("string hash: "+hashing.mhash(target))
|
file = open(file, 'r')
|
||||||
|
target = file.read()
|
||||||
|
file.close()"""
|
||||||
|
print("single character hash: "+repr(hashing.ohash(target)))
|
||||||
|
print("string hash: "+repr(hashing.mhash(target)))
|
||||||
|
print("decoding into decimal...")
|
||||||
|
out = []
|
||||||
|
for i in hashing.mhash(target):
|
||||||
|
out.append(characters.index(i))
|
||||||
|
print("decoded! "+str(out))
|
||||||
|
print("decoding to bin...")
|
||||||
|
nout = []
|
||||||
|
for i in out:
|
||||||
|
nout.append(hashing.tobin(i))
|
||||||
|
print("decoded! "+str(nout))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue