Return a mapping of hexdigest checksum strings keyed by checksum algorithm name from hashing the content of the file at ``location``. Use the ``checksum_names`` list of checksum names. The mapping is guaranted to contains all the requested names as keys. If the location is not a file,
(location, checksum_names=("md5", "sha1", "sha256", "sha512", "sha1_git"))
| 281 | |
| 282 | |
| 283 | def multi_checksums(location, checksum_names=("md5", "sha1", "sha256", "sha512", "sha1_git")): |
| 284 | """ |
| 285 | Return a mapping of hexdigest checksum strings keyed by checksum algorithm name from hashing the |
| 286 | content of the file at ``location``. Use the ``checksum_names`` list of checksum names. The |
| 287 | mapping is guaranted to contains all the requested names as keys. If the location is not a file, |
| 288 | or if the file is empty, the values are None. |
| 289 | |
| 290 | The purpose of this function is to avoid read the same file multiple times |
| 291 | to compute different checksums. |
| 292 | """ |
| 293 | if not filetype.is_file(location): |
| 294 | return {name: None for name in checksum_names} |
| 295 | file_size = get_file_size(location) |
| 296 | if file_size == 0: |
| 297 | return {name: None for name in checksum_names} |
| 298 | |
| 299 | hashers = { |
| 300 | name: get_hasher_instance_by_name(name=name, total_length=file_size) |
| 301 | for name in checksum_names |
| 302 | } |
| 303 | |
| 304 | for chunk in binary_chunks(location): |
| 305 | for hasher in hashers.values(): |
| 306 | hasher.update(msg=chunk) |
| 307 | |
| 308 | return {name: hasher.hexdigest() for name, hasher in hashers.items()} |