tangetools/whitehash/whitehash
2022-07-01 03:20:25 +08:00

84 lines
2.1 KiB
Python
Executable file

#!/usr/bin/python3
# Replace \n\t with \n(spaces)\t at random to get a nice sha1sum
#
# SYNOPSIS
#
# whitehash n < file
#
# n = number of chars
# file = file to sha1sum
import sys
import math
from random import shuffle
import hashlib
searchlen = int(sys.argv[1])
def readparts():
# Read file
part = []
# Block of text ending in \n that is followed by a \t in next section
section = ""
for i in sys.stdin:
if i[0:1] == "\t":
part.append(section)
section = ""
section += i
part.append(section)
# part[1..] starts with \t
return(part)
def mergeparts(tabs,part):
# merge parts at random so we have exactly tabs number of parts
x = [i for i in range(1,len(part))]
shuffle(x)
x = x[0:tabs]
x.sort();
x[:0] = [0]
x.append(len(part))
p2 = []
for i in range(len(x)-1):
p2.append(("".join(part[x[i]:x[i+1]])).encode("utf-8"))
return(p2)
spc = [ i.encode("utf-8")
for i in ("", " ", " ", " ", " ", " ", " ", " ") ]
def recur(m,pre,n):
if n == len(part)-1:
for i in spc:
sub = m.copy()
sub.update(i+part[n])
if (sub.hexdigest())[0:searchlen] in searchstrings:
print(sub.hexdigest())
print((pre+i+part[n]).decode())
else:
for i in spc:
sub = m.copy()
sub.update(i+part[n])
recur(sub,pre+i+part[n],n+1)
bits = searchlen*4
part = readparts();
tabs = int(math.ceil(bits/3.0))
if tabs > len(part)-1:
print("Too few tabs: %s hex values is %s bits which needs %d tabs and there are only %s"
% (searchlen,bits,tabs,len(part)))
exit(1);
part = mergeparts(tabs,part)
s = "0123456789abcdef"
# Chop at searchlen
searchstrings = [ i[0:searchlen] for i in
# Generate 111..1 222..2 .. fff..f
["%016x"%(0x1111111111111111*i) for i in range(16)]
+
# Generate 012..f 123..0 .. f01..e
[ s[-i:]+s[:-i] for i in range(16)] ]
m = hashlib.sha1()
m.update(part[0])
recur(m,part[0],1)