os_helper.hash_utils module

Hashing Utilities

This module provides functions to perform hashing of strings, files, and entire folders. It supports optional date stamping, partial content hashing, and path-based hashing.

Author:
os_helper.hash_utils.hash_string(s, size=-1)[source]

Generate a hash of a given string and optionally returns a truncated version.

Parameters:
  • s (str) – The input string to hash.

  • size (int, optional) – If positive, truncates the hash to the specified length. Defaults to -1 (no truncation).

Returns:

The hashed string, optionally truncated.

Return type:

str

Example

>>> isinstance(hash_string("example"), str)
True
>>> len(hash_string("example"))
40
>>> len(hash_string("example", size=8))
8

Note

The exact digest depends on the underlying hash engine (RIPEMD-160 when available, BLAKE2b truncated to 20 bytes otherwise). The output length stays 40 hex characters either way.

os_helper.hash_utils.hashfile(path, hash_content=True, date=False)[source]

Generate a hash for a file’s content and/or its last modification date.

Parameters:
  • path (str) – The path to the file to hash.

  • hash_content (bool, optional) – If True, includes the file’s content in the hash (default: True).

  • date (bool, optional) – If True, includes the current date in the hash (default: False).

Returns:

The resulting hash of the file as a 40-character hex string.

Return type:

str

os_helper.hash_utils.hashfolder(path, hash_content=True, hash_path=False, date=False)[source]

Generate a hash for the contents of a folder and/or its path.

Parameters:
  • path (str) – The path to the folder to hash.

  • hash_content (bool, optional) – If True, includes the folder’s contents in the hash (default: True).

  • hash_path (bool, optional) – If True, includes the folder’s path in the hash (default: False).

  • date (bool, optional) – If True, includes the current date in the hash (default: False).

Returns:

The resulting hash of the folder and/or its contents as a 40-character hex string.

Return type:

str