Skip to content

Commit

Permalink
Add readable hashing function
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Oct 18, 2024
1 parent 1e1bfdc commit 00fb891
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions fmtr/tools/hash_tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import base64
import hashlib
from zlib import crc32

from fmtr.tools.config import ToolsConfig

SPECIALS = {'O': '9', '=': '9', 'I': '9'}

def hash_unit(value: str) -> float:
"""
Expand All @@ -11,3 +14,17 @@ def hash_unit(value: str) -> float:
"""
value = str(value).encode(ToolsConfig.ENCODING)
return float(crc32(value) & 0xffffffff) / 2 ** 32


def get_hash_readable(string, length=None):
"""
Get hash optimised for information density and readability.
"""
hash_sha1 = hashlib.sha1(string.encode()).digest()
hash_b32 = base64.b32encode(hash_sha1).decode()
value = hash_b32[:length]
for old, new in SPECIALS.items():
value = value.replace(old, new)
return value
2 changes: 1 addition & 1 deletion fmtr/tools/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.8
0.9.9

0 comments on commit 00fb891

Please sign in to comment.