Skip to content

Instantly share code, notes, and snippets.

@RemyPorter
Last active August 29, 2015 14:05
Show Gist options
  • Save RemyPorter/93758aacbe5c96f31545 to your computer and use it in GitHub Desktop.
Save RemyPorter/93758aacbe5c96f31545 to your computer and use it in GitHub Desktop.
Friendly Hash Printing
import linecache
import math
def wordhash(hash, wordlist="all.short", address_size=4):
words = []
for i in range(0, len(hash), address_size):
hx = "00"
try:
hx = hash[i:i+address_size]
except IndexError as err:
hx = hash[-address_size:0]
line = int(hx, 16)
word = linecache.getline(wordlist, line).strip()
words += [word]
return " ".join(words)
class FriendlyHash(str):
def __new__(cls, value, worder=wordhash):
instance = super().__new__(cls, value)
instance.__friendly = False
instance.__text = ""
return instance
@property
def friendly(self):
if not self.__friendly:
self.__text = wordhash(self)
self.__friendly = True
return self.__text
>>> f = hashwords.FriendlyHash("7227e226bccd026beb603e866e68175d05922545")
>>> f.friendly
'jobbers thrown sabretache adjunctive unbitting donate insure bobsleds alluding chittered'
@RemyPorter
Copy link
Author

I slapped together a function that pulls from a list of 65536 English words to make a more memorable and distinctive (but equally unique!) representation of a SHA-1 hash. This'll be something I work into the Plain Text Workflow, as authors aren't going to want to try and understand long hexidecimal strings.

It'd be nice if I could use memory mapped files, but linecache is going to have to do for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment