MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / multi_checksums

Function multi_checksums

src/commoncode/hash.py:283–308  ·  view source on GitHub ↗

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"))

Source from the content-addressed store, hash-verified

281
282
283def 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()}

Callers 7

get_checksumsMethod · 0.90
get_file_infoFunction · 0.90
test_multi_checksumsMethod · 0.90

Calls 6

get_file_sizeFunction · 0.85
binary_chunksFunction · 0.85
hexdigestMethod · 0.80
valuesMethod · 0.45
updateMethod · 0.45