File Hashers
dorsal.file.utils.quick_hasher.QuickHasher
Generate a 'quick hash' for large files by sampling content chunks.
- Designed for speed on large files.
- Provides a deterministic hash for fast lookups.
- Not cryptographically collision-resistant like full-file hashes.
- Aims for strong probabilistic uniqueness.
- Number of chunks sampled varies with file size.
- Sampling is deterministic, seeded by file size (using modulo operation), ensuring the same file (by size and content at sampled locations) will always produce the same QuickHash.
hash
Generate a 'quick hash' by sampling file content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
* file_path
|
Absolute path to the file. |
required | |
* file_size
|
File size in bytes. |
required | |
* raise_on_filesize_error
|
If True, raise ValueError if file size is outside permitted range. Default False (returns None). |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
|
str | None
|
|
Raises:
| Type | Description |
|---|---|
* OSError
|
For file access errors (e.g., FileNotFoundError, PermissionError). |
* ValueError
|
If |
* QuickHashFileInstabilityError
|
If the file changes state during hashing. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/utils/quick_hasher.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
dorsal.file.utils.file_hasher.FileHasher
Calculate cryptographic hashes for a given file by reading it in chunks.
Supports standard hashes like SHA-256 and BLAKE3, and can optionally
calculate a TLSH (Trend Micro Locality Sensitive Hash) similarity hash if the
tlsh library is available and the file meets minimum size requirements.
Initializes the FileHasher.
Source code in venv/lib/python3.13/site-packages/dorsal/file/utils/file_hasher.py
hash
Calculates multiple hashes for the specified file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The absolute path to the file to be hashed. |
required |
file_size
|
int
|
The size of the file in bytes. This must be provided by the caller. |
required |
calculate_tlsh
|
bool
|
If True, attempts to calculate the TLSH similarity hash.
Requires |
True
|
Returns:
| Type | Description |
|---|---|
dict[HashFunctionId, str]
|
A dictionary mapping hash algorithm names (e.g., "SHA-256", "BLAKE3", "TLSH") |
dict[HashFunctionId, str]
|
to their hexadecimal string representations. If a hash cannot be |
dict[HashFunctionId, str]
|
calculated (e.g., TLSH due to size or missing library), it will be |
dict[HashFunctionId, str]
|
omitted from the dictionary. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
PermissionError
|
If the file cannot be read due to permissions. |
IOError
|
For other I/O related errors during file reading. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/utils/file_hasher.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
hash_blake3
Calculates the BLAKE3 hash for a single file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The absolute path to the file to be hashed. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The hexadecimal BLAKE3 hash as a string. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/utils/file_hasher.py
hash_sha256
Calculates the SHA-256 hash for a single file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The absolute path to the file to be hashed. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The hexadecimal SHA-256 hash as a string. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/utils/file_hasher.py
hash_tlsh
Calculates the TLSH similarity hash for a single file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The absolute path to the file to be hashed. |
required |
file_size
|
int
|
The size of the file in bytes. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The TLSH hash as a string, or None if the tlsh library is not |
str | None
|
available, the file is too small, the file cannot be read, or the |
str | None
|
data has insufficient variance for TLSH. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/utils/file_hasher.py
dorsal.file.utils.get_blake3_hash
dorsal.file.utils.get_quick_hash
Get the quick hash of a file.
When fallback_to_sha256 is True, when QuickHasher fails (e.g. the file is too small)
a SHA-256 hash is calculated and returned in its place.