Coverage for pyguymer3/sha256.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def sha256( 

5 fname, 

6 /, 

7 *, 

8 chunksize = 1048576, 

9): 

10 """Find the SHA-256 hash of a file 

11 

12 This function returns the SHA-256 hash of the passed file. 

13 

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) 

20 

21 Returns 

22 ------- 

23 hexdigest : str 

24 The hash hexdigest of the input file. 

25 

26 Notes 

27 ----- 

28 Copyright 2017 Thomas Guymer [1]_ 

29 

30 References 

31 ---------- 

32 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

33 """ 

34 

35 # Import standard modules ... 

36 import hashlib 

37 

38 # Create hash object ... 

39 hobj = hashlib.sha256() 

40 

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) 

47 

48 # Stop looping if this chunk is empty ... 

49 if len(chunk) == 0: 

50 break 

51 

52 # Update hash object with chunk ... 

53 hobj.update(chunk) 

54 

55 # Return answer ... 

56 return hobj.hexdigest()