Skip to content

Instantly share code, notes, and snippets.

@averagehuman
Created October 3, 2014 10:16
Show Gist options
  • Save averagehuman/321709b277402979380d to your computer and use it in GitHub Desktop.
Save averagehuman/321709b277402979380d to your computer and use it in GitHub Desktop.
"""
Use for anonymizing strings not for cryptographic purposes
Makes use of the following identities::
ord('1') % 48 == ord('a') % 48 == 1
ord('2') % 48 == ord('b') % 48 == 2
...
ord('6') % 48 == ord('f') % 48 == 6
There are 6 orders of magnitude less possible 32-character digests in base 10 than base 16.
"""
from hashlib import md5
from random import random
def base10digest(text, length=32, chunk=None, sep='-', salt=None, force=False):
if not force and text.isdigit():
# if it is numeric already just return same text
return text
salt = salt or str(random())
chars = ''.join(
str(ord(x) % 48) for x in md5(text + salt).hexdigest()[:length]
)
if chunk:
chars = sep.join(chars[i:i+chunk] for i in range(0, length, chunk))
return chars
if __name__ == '__main__':
print(base10digest(''))
print(base10digest('', length=20))
print(base10digest('', length=20, chunk=4))
print(base10digest('', length=21, chunk=3, sep='#'))
print(base10digest('', length=22, chunk=3, sep='#', salt='asdf'))
print(base10digest('12345789273637'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment