edq.util.hash

 1import hashlib
 2import typing
 3
 4import edq.util.dirent
 5
 6DEFAULT_CLIP_HASH_LENGTH: int = 8
 7
 8def sha256_hex(payload: typing.Any, encoding: str = edq.util.dirent.DEFAULT_ENCODING) -> str:
 9    """ Compute and return the hex string of the SHA3-256 encoding of the payload. """
10
11    if (isinstance(payload, str)):
12        payload = payload.encode(encoding)
13
14    digest = hashlib.new('sha256')
15    digest.update(payload)
16    return digest.hexdigest()
17
18def clip_text(text: str, max_length: int, hash_length: int = DEFAULT_CLIP_HASH_LENGTH) -> str:
19    """
20    Return a clipped version of the input text that is no longer than the specified length.
21    If the base text is found to be too long,
22    then enough if the tail of the text will be removed to insert a note about the clipping
23    and the first |hash_length| characters of the hash from sha256_hex().
24
25    Note that the max length is actually a soft cap.
26    Longer strings can be generated if the original text is shorter than the notification
27    that will be inserted into the clipped text.
28    """
29
30    if (len(text) <= max_length):
31        return text
32
33    hash_hex = sha256_hex(text)
34    notification = f"[text clipped {hash_hex[0:hash_length]}]"
35
36    # Don't clip the text if the final string would be longer.
37    if (len(notification) >= len(text)):
38        return text
39
40    keep_length = max(0, max_length - len(notification))
41    return text[0:keep_length] + notification
DEFAULT_CLIP_HASH_LENGTH: int = 8
def sha256_hex(payload: Any, encoding: str = 'utf-8') -> str:
 9def sha256_hex(payload: typing.Any, encoding: str = edq.util.dirent.DEFAULT_ENCODING) -> str:
10    """ Compute and return the hex string of the SHA3-256 encoding of the payload. """
11
12    if (isinstance(payload, str)):
13        payload = payload.encode(encoding)
14
15    digest = hashlib.new('sha256')
16    digest.update(payload)
17    return digest.hexdigest()

Compute and return the hex string of the SHA3-256 encoding of the payload.

def clip_text(text: str, max_length: int, hash_length: int = 8) -> str:
19def clip_text(text: str, max_length: int, hash_length: int = DEFAULT_CLIP_HASH_LENGTH) -> str:
20    """
21    Return a clipped version of the input text that is no longer than the specified length.
22    If the base text is found to be too long,
23    then enough if the tail of the text will be removed to insert a note about the clipping
24    and the first |hash_length| characters of the hash from sha256_hex().
25
26    Note that the max length is actually a soft cap.
27    Longer strings can be generated if the original text is shorter than the notification
28    that will be inserted into the clipped text.
29    """
30
31    if (len(text) <= max_length):
32        return text
33
34    hash_hex = sha256_hex(text)
35    notification = f"[text clipped {hash_hex[0:hash_length]}]"
36
37    # Don't clip the text if the final string would be longer.
38    if (len(notification) >= len(text)):
39        return text
40
41    keep_length = max(0, max_length - len(notification))
42    return text[0:keep_length] + notification

Return a clipped version of the input text that is no longer than the specified length. If the base text is found to be too long, then enough if the tail of the text will be removed to insert a note about the clipping and the first |hash_length| characters of the hash from sha256_hex().

Note that the max length is actually a soft cap. Longer strings can be generated if the original text is shorter than the notification that will be inserted into the clipped text.