Skip to content

confingy.utils.hashing#

hashing #

hash_class #

hash_class(cls: type, algorithm: str = 'sha256') -> str

Create a hash of a class based on its bytecode. Only changes that affect code execution will change the hash.

Parameters:

Name Type Description Default
cls type

The class to hash

required
algorithm str

The hashing algorithm to use (default: 'sha256')

'sha256'

Returns:

Type Description
str

Hexadecimal hash string

Source code in src/confingy/utils/hashing.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def hash_class(cls: type, algorithm: str = "sha256") -> str:
    """
    Create a hash of a class based on its bytecode.
    Only changes that affect code execution will change the hash.

    Args:
        cls: The class to hash
        algorithm: The hashing algorithm to use (default: 'sha256')

    Returns:
        Hexadecimal hash string
    """
    hasher = hashlib.new(algorithm)

    # Get all components that affect execution
    components = _get_class_bytecode_components(cls)

    # Sort components by name for deterministic hashing
    components.sort(key=lambda x: x[0])

    # Hash each component
    for name, data in components:
        hasher.update(name.encode("utf-8"))
        hasher.update(b":::")  # Separator
        hasher.update(data)
        hasher.update(b"|||")  # Component separator

    return hasher.hexdigest()