Module

utils.hashing

Cryptographic hashing utilities for Bengal.

Provides standardized hashing for file fingerprinting, cache keys, and content-addressable storage.

Example:

from bengal.utils.hashing import hash_str, hash_file, hash_dict

# Hash string content
key = hash_str("hello world")  # "b94d27b9..."

# Hash with truncation (for fingerprints)
fingerprint = hash_str("hello world", truncate=8)  # "b94d27b9"

# Hash file content
file_hash = hash_file(Path("content/post.md"))

# Hash dict deterministically
config_hash = hash_dict({"key": "value", "nested": [1, 2, 3]})

Related Modules:

  • bengal.cache.build_cache: Uses for file fingerprinting
  • bengal.core.asset: Asset fingerprinting

Functions

hash_str
Hash string content using specified algorithm.
3 str
def hash_str(content: str, truncate: int | None = None, algorithm: str = 'sha256') -> str

Hash string content using specified algorithm.

Parameters 3

Name Type Default Description
content str

String content to hash

truncate int | None None

Truncate result to N characters (None = full hash)

algorithm str 'sha256'

Hash algorithm ('sha256', 'md5')

Returns

str

Hex digest of hash, optionally truncated

hash_bytes
Hash bytes content using specified algorithm.
3 str
def hash_bytes(content: bytes, truncate: int | None = None, algorithm: str = 'sha256') -> str

Hash bytes content using specified algorithm.

Parameters 3

Name Type Default Description
content bytes

Bytes content to hash

truncate int | None None

Truncate result to N characters (None = full hash)

algorithm str 'sha256'

Hash algorithm ('sha256', 'md5')

Returns

str

Hex digest of hash, optionally truncated

hash_dict
Hash dictionary deterministically (sorted keys, string serialization).
3 str
def hash_dict(data: dict[str, Any], truncate: int | None = 16, algorithm: str = 'sha256') -> str

Hash dictionary deterministically (sorted keys, string serialization).

Parameters 3

Name Type Default Description
data dict[str, Any]

Dictionary to hash

truncate int | None 16

Truncate result to N characters (default: 16)

algorithm str 'sha256'

Hash algorithm ('sha256', 'md5')

Returns

str

Hex digest of hash

hash_file
Hash file content by streaming (memory-efficient for large files).
4 str
def hash_file(path: Path, truncate: int | None = None, algorithm: str = 'sha256', chunk_size: int = 8192) -> str

Hash file content by streaming (memory-efficient for large files).

Parameters 4

Name Type Default Description
path Path

Path to file

truncate int | None None

Truncate result to N characters (None = full hash)

algorithm str 'sha256'

Hash algorithm ('sha256', 'md5')

chunk_size int 8192

Read buffer size in bytes

Returns

str

Hex digest of file content hash

hash_file_with_stat
Hash file for fingerprinting (includes mtime for fast invalidation). Combines file content hash wi…
3 str
def hash_file_with_stat(path: Path, truncate: int | None = 8, algorithm: str = 'sha256') -> str

Hash file for fingerprinting (includes mtime for fast invalidation).

Combines file content hash with modification time for efficient cache invalidation without re-hashing unchanged files.

Parameters 3

Name Type Default Description
path Path

Path to file

truncate int | None 8

Truncate result to N characters (default: 8 for URLs)

algorithm str 'sha256'

Hash algorithm

Returns

str

Fingerprint string suitable for URLs