made code importable as a library
This commit is contained in:
parent
e7121ee9d6
commit
591bade711
|
@ -6,10 +6,13 @@ created because I wanted a consistent hash equivalence thingy and only found som
|
|||
|
||||
`Usage: wordmap.py <wordlist file> <word to hash>`
|
||||
|
||||
for example `python3 wordmap.py wordlists/corncob_lowercase.txt "example words"` would result in `serenading`
|
||||
for example `python3 wordmap.py wordlists/corncob_lowercase.txt "example words"` would result in `serenading`
|
||||
and to use the terminal colors, on *nix-like systems you can use `(i="example";python3 wordmap.py wordlists/term16color.txt "$i"|tr -d \\n;printf "%s\33[0m\n" "$i")`
|
||||
|
||||
it can also be imported, something like `python3 -c 'a="example";from wordmap import wordmap;wordmap("wordlists/term16color.txt",a);print(a+"\x1b[0m")'`
|
||||
|
||||
corncob lists included here are from [http://www.mieliestronk.com/wordlist.html](http://www.mieliestronk.com/wordlist.html)
|
||||
the colors list is from wikipedia, more specifically re-parsed from [https://raw.githubusercontent.com/codebrainz/color-names/master/output/colors.conf](https://raw.githubusercontent.com/codebrainz/color-names/master/output/colors.conf)
|
||||
the color names list is from wikipedia, more specifically re-parsed from [https://raw.githubusercontent.com/codebrainz/color-names/master/output/colors.conf](https://raw.githubusercontent.com/codebrainz/color-names/master/output/colors.conf)
|
||||
|
||||
but you can use any newline-separated list of strings
|
||||
|
||||
|
|
40
wordmap.py
40
wordmap.py
|
@ -5,41 +5,35 @@
|
|||
from hashlib import blake2b as blake2
|
||||
from sys import argv
|
||||
|
||||
try:
|
||||
if argv[1]=="-h" or argv[1]=="--help": raise IndexError
|
||||
wordlist_file=argv[1]
|
||||
input=argv[2]
|
||||
except IndexError:
|
||||
print(f"Usage: {argv[0]} <wordlist file> <word to hash>")
|
||||
quit(1)
|
||||
|
||||
def load_words():
|
||||
def load_words(wordlist_file: str):
|
||||
with open(wordlist_file) as word_file:
|
||||
valid_words = sorted(set(word_file.read().split("\n")))
|
||||
return valid_words
|
||||
|
||||
|
||||
def scale(val, src, dst):
|
||||
"""
|
||||
Scale the given value from the scale of src to the scale of dst.
|
||||
"""
|
||||
# Scale the given value from the scale of src to the scale of dst.
|
||||
return ((val - src[0]) / (src[1]-src[0])) * (dst[1]-dst[0]) + dst[0]
|
||||
|
||||
if __name__ == '__main__':
|
||||
words = load_words()
|
||||
#if __name__ == '__main__':
|
||||
def wordmap(wordlist_file: str,input: str):
|
||||
words = load_words(wordlist_file)
|
||||
words_max=len(words)
|
||||
digest_size=4 #in bytes
|
||||
#slightly surprised this works
|
||||
digest_max=int.from_bytes(b'\xff'*digest_size,"big")
|
||||
hash=blake2(digest_size=digest_size)
|
||||
hash.update(bytes(input,"utf-8"))
|
||||
#input_hash_hex=hash.hexdigest()
|
||||
#input_hash_raw=hash.digest()
|
||||
#input_hash=int.from_bytes(input_hash_raw, "big")
|
||||
input_hash=int(hash.hexdigest(),16)
|
||||
#print(f"{hash.name} w {hash.digest_size*8}-bit digest\n from word {input}\n {input_hash}")
|
||||
#print(f"word list length is {words_max}, digest's max possible length is {digest_max}, attempting to map")
|
||||
input_hash_mapped=round(scale(input_hash,(0,digest_max),(0,words_max)))
|
||||
#print(f"lineraly mapped hash: {input_hash_mapped}")
|
||||
print(words[input_hash_mapped])
|
||||
#print(words[b])
|
||||
print(words[input_hash_mapped],end="")
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
if argv[1]=="-h" or argv[1]=="--help": raise IndexError
|
||||
wordlist_file=argv[1]
|
||||
input=argv[2]
|
||||
except IndexError:
|
||||
print(f"Usage: {argv[0]} <wordlist file> <word to hash>")
|
||||
quit(1)
|
||||
wordmap(wordlist_file,input)
|
||||
print()
|
||||
|
|
Loading…
Reference in New Issue