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:
Warith HARCHAOUI, https://linkedin.com/in/warith-harchaoui
- os_helper.hash_utils.hash_string(s, size=-1)[source]
Generate a hash of a given string and optionally returns a truncated version.
- Parameters:
- Returns:
The hashed string, optionally truncated.
- Return type:
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:
- Returns:
The resulting hash of the file as a 40-character hex string.
- Return type:
- 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: