Coverage for pyguymer3/sha512.py: 100%
10 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def sha512(
5 fname,
6 /,
7 *,
8 chunksize = 1048576,
9):
10 """Find the SHA-512 hash of a file
12 This function returns the SHA-512 hash of the passed file.
14 Parameters
15 ----------
16 fname : str
17 the input file name
18 chunksize : int, optional
19 the size of the chunks of any files which are read in (in bytes)
21 Returns
22 -------
23 hexdigest : str
24 The hash hexdigest of the input file.
26 Notes
27 -----
28 Copyright 2017 Thomas Guymer [1]_
30 References
31 ----------
32 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
33 """
35 # Import standard modules ...
36 import hashlib
38 # Create hash object ...
39 hobj = hashlib.sha512()
41 # Open input file as bytes ...
42 with open(fname, "rb") as fObj:
43 # Start infinite loop ...
44 while True:
45 # Read a chunk ...
46 chunk = fObj.read(chunksize)
48 # Stop looping if this chunk is empty ...
49 if len(chunk) == 0:
50 break
52 # Update hash object with chunk ...
53 hobj.update(chunk)
55 # Return answer ...
56 return hobj.hexdigest()