O(K)hash
Introduction
O(K)hash (pronounced OK hash or oꭓash) is a hash function with $O(1)$ complexity based on SHA-256. It is designed to efficiently calculate hashes for large files by reading only a fixed subset of data from random positions within the file. The 'K' parameter denotes the strength of the hashing, with lower values reading less data. For example, a hash with K=1 reads only 1KiB of data, while a hash with K=2 reads 1MiB of data. The hash results are downgradable, meaning a hash of strength K=3 can validate a file hash calculated with K=1.
Rational
Calculating the hash of a large file normally involves reading the entire file, which can be time-consuming, especially with slow I/O operations. O(K)hash addresses this issue by reading a fixed number of bytes from random positions inside the file, providing a constant time complexity of $O(1)$. This approach generates a reliable fingerprint for identifying duplicate files and validating files efficiently.
Limitations
After all, it's just an OK hash:
- Not Suitable for Detecting Corruptions: O(K)hash is not suitable for detecting file corruptions that do not change the size of the file. In the case of a bit flip or small corruptions, the probability of detecting corruption is lower than: $\frac{base size}{file size}$.
- Consider File Size Checking: Depending on the nature and number of large files you are working with, it may be more effective to check the file size before calculating a conventional hash to ensure data integrity.
Quickstart
Installation
You can install O(K)hash using pip:
pip install okhash
Usage
-
Calculate O(K)Hash of a String:
import okhash data = "Hello, world!" checksum = okhash.okhash(data.encode('utf-8'), K=3) print(checksum.hex())
-
Calculate & compare O(K)Hash of Files:
import okhash file1_checksum = okhash.okhash_filepath("file1.bin") file2_checksum = okhash.okhash_filepath("file2.bin") if okhash.compare_okhashes(file1_checksum, file2_checksum): print("Checksums match.") else: print("Checksums do not match.")
Command Line Usage
-
Calculate Checksums: To calculate checksums for a file with a specified K value (default is K=2), use the following command:
python3 -m okhash -K 3 file.bin
-
Check Checksums: The result of the previous command can be used to check checksums for multiple files:
python3 -m okhash *.bin > okhashes.txt python3 -m okhash --check okhashes.txt
-
Additional Options:
python3 -m okhash --help
The Strengths (K)
Here's a table describing the strengths (K) and their corresponding parameters:
| K | Base Size (Subset Data Size for Hash Calculation) | Block Size |
|---|---|---|
| 1 | 1024 B = 1 KiB | 1024 B |
| 2 | 1048576 B = 1 MiB | 4096 B |
| 3 | 1073741824 B = 1 GiB | 262144 B |
| 4 | 1099511627776 B = 1 TiB | 16777216 B |
| K | $2^{10K}$ | $1024 \times \lceil \frac{2^{6K}}{1024} \rceil$ |
The minimum file size for a given K is equal to twice the base size; otherwise, the hash calculation will resort to SHA-256 for the entire file.
License
O(K)hash is released under the MIT License.
Metadata
Release files for okhash 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| okhash-1.0.tar.gz | 7.0 kB | Without provenance | Details |
Release files / okhash-1.0.tar.gz
| Download URL | okhash-1.0.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8621fe0c31daf50c8acd4490a2c4a6d61d6d375d232c7856b44884892d6ccfa2
|
|
BLAKE2b-256 checksum How to use checksums |
d7ca9d07b37cb5a7d4fb22c564ff4684ca95b2084c52d14fc89a71dbd1692a16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Security for okhash 1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.