docs for muutils v0.8.10
View Source on GitHub

muutils.misc.hashing


 1from __future__ import annotations
 2
 3import base64
 4import hashlib
 5import json
 6
 7
 8def stable_hash(s: str | bytes) -> int:
 9    """Returns a stable hash of the given string. not cryptographically secure, but stable between runs"""
10    # init hash object and update with string
11    s_bytes: bytes
12    if isinstance(s, str):
13        s_bytes = s.encode("utf-8")
14    else:
15        s_bytes = s
16    hash_obj: hashlib._Hash = hashlib.md5(s_bytes)
17    # get digest and convert to int
18    return int.from_bytes(hash_obj.digest(), "big")
19
20
21def stable_json_dumps(d) -> str:
22    return json.dumps(
23        d,
24        sort_keys=True,
25        indent=None,
26    )
27
28
29def base64_hash(s: str | bytes) -> str:
30    """Returns a base64 representation of the hash of the given string. not cryptographically secure"""
31    s_bytes: bytes
32    if isinstance(s, str):
33        s_bytes = bytes(s, "UTF-8")
34    else:
35        s_bytes = s
36    hash_bytes: bytes = hashlib.md5(s_bytes).digest()
37    hash_b64: str = base64.b64encode(hash_bytes, altchars=b"-_").decode()
38    return hash_b64

def stable_hash(s: str | bytes) -> int:
 9def stable_hash(s: str | bytes) -> int:
10    """Returns a stable hash of the given string. not cryptographically secure, but stable between runs"""
11    # init hash object and update with string
12    s_bytes: bytes
13    if isinstance(s, str):
14        s_bytes = s.encode("utf-8")
15    else:
16        s_bytes = s
17    hash_obj: hashlib._Hash = hashlib.md5(s_bytes)
18    # get digest and convert to int
19    return int.from_bytes(hash_obj.digest(), "big")

Returns a stable hash of the given string. not cryptographically secure, but stable between runs

def stable_json_dumps(d) -> str:
22def stable_json_dumps(d) -> str:
23    return json.dumps(
24        d,
25        sort_keys=True,
26        indent=None,
27    )
def base64_hash(s: str | bytes) -> str:
30def base64_hash(s: str | bytes) -> str:
31    """Returns a base64 representation of the hash of the given string. not cryptographically secure"""
32    s_bytes: bytes
33    if isinstance(s, str):
34        s_bytes = bytes(s, "UTF-8")
35    else:
36        s_bytes = s
37    hash_bytes: bytes = hashlib.md5(s_bytes).digest()
38    hash_b64: str = base64.b64encode(hash_bytes, altchars=b"-_").decode()
39    return hash_b64

Returns a base64 representation of the hash of the given string. not cryptographically secure