Convenience hashing facilities.
Project description
Convenience hashing facilities.
Latest release 20250414.1: Update the module docstring.
This predefines classes for various hash algorithms:
BLAKE3 (if we can import blake3), MD5, SHA1, SHA224,
SHA256, SHA384, SHA512, and a BaseHashCode base class
as a common ancestor with a .hashclass() factory method for
creating new subclasses for other algorithms.
All BaseHashCode classes have a variety of convenience factories for making instances:
from_hashbytes: from an existing digestbytesfrom_hashbytes_hex: from a hex string of an existing digestfrom_named_hashbytes_hex: from a hashname (eg'blake3') and a hex stringfrom_named_hashbytes_hex: from a hashname:hex stringfrom_data: frombytescontentfrom_buffer: from aCornuCopyBufferor anything which can be promoted to onefrom_fspath: from a filesystem path
Hashing some content returns instances of these classes, which
are subclasses of bytes i.e. the digest. All BaseHashCode classes various methods:
str(hashcode): returns a hashname:hex string.hashname: the hash function name.hex(): the digest as a hex string
The common example:
# obtain the contwnt hash from the file `fspath`
hashcode = hashclass.from_fspath(fspath)
where hashclass is whatever BaseHashCode subclass is in use.
Module contents:
-
ClassBaseHashCode(builtins.bytes): Base class for hashcodes, subclassed bySHA1,SHA256` et al.You can obtain the class for a particular hasher by name, example:
SHA256 = BaseHashCode.hashclass('sha256')
BaseHashCode.__str__(self):
Return f'{self.hashname}:{self.hex()}'.
BaseHashCode.from_buffer(bfr: cs.buffer.CornuCopyBuffer):
Compute hashcode from the contents of the CornuCopyBuffer bfr.
BaseHashCode.from_data(bs):
Compute hashcode from the data bs.
BaseHashCode.from_fspath(fspath, **kw):
Compute hashcode from the contents of the file fspath.
BaseHashCode.from_hashbytes(hashbytes):
Factory function returning a BaseHashCode object from the hash bytes.
BaseHashCode.from_hashbytes_hex(hashhex: str):
Factory function returning a BaseHashCode object
from the hash bytes hex text.
BaseHashCode.from_named_hashbytes_hex(hashname, hashhex):
Factory function to return a BaseHashCode object
from the hash type name and the hash bytes hex text.
BaseHashCode.from_prefixed_hashbytes_hex(hashtext: str):
Factory function returning a BaseHashCode object
from the hash bytes hex text prefixed by the hashname.
This is the reverse of __str__.
BaseHashCode.get_hashfunc(hashname: str):
Fetch the hash function implied by hashname.
BaseHashCode.hashclass(hashname: str, hashfunc=None, **kw):
Return the class for the hash function named hashname.
Parameters:
hashname: the name of the hash functionhashfunc: optional hash function for the class
BaseHashCode.hashname:
The hash code type name, derived from the class name.
BaseHashCode.hex(self) -> str:
Return the hashcode bytes transcribes as a hexadecimal ASCII str.
BaseHashCode.promote(obj):
Promote to a BaseHashCode instance.
BLAKE3.hashfunc
MD5.hashfunc
SHA1.hashfunc
SHA224.hashfunc
SHA256.hashfunc
SHA384.hashfunc
SHA512.hashfunc
Release Log
Release 20250414.1: Update the module docstring.
Release 20250414: Define BLAKE3 if we can import blake3.
Release 20241207: BaseHashCode.hashclass: raise ValueError from unknown hash function name with greater detail on the underlying failure.
Release 20240412:
- BaseHashCode.hashclass(hashname): fall back to looking for blake3 from the blake3 module.
- BaseHashCode: new get_hashfunc(hashname) static method.
Release 20240316: Fixed release upload artifacts.
Release 20240211: Initial PyPI release: BaseHashCode(bytes) and subclasses for various hash algorithms.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cs_hashutils-20250414.1.tar.gz.
File metadata
- Download URL: cs_hashutils-20250414.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
857f1fd8fa804aa2134337f1bb6377d1fdfd3bf4d668d8f7d095c3a9c12eece8
|
|
| MD5 |
8fcd1b89d2c2da7794d0d09b06f2e938
|
|
| BLAKE2b-256 |
9183ea52f4cda78564cc921ee982911bdc02a12754ef9e75644a87c474e2ace5
|
File details
Details for the file cs_hashutils-20250414.1-py2.py3-none-any.whl.
File metadata
- Download URL: cs_hashutils-20250414.1-py2.py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb68b9a4d6bafa158b69f229469ca6d4f0b3ba9e138fb977337b4ca95a4216d7
|
|
| MD5 |
31fd6dc99610483f40e26794cc46fd98
|
|
| BLAKE2b-256 |
7f882b94abaca1bf16b68973cdcd6f06c8abd4a151d5306d12dff267d71b84d6
|