Coverage for src/ensae_teaching_cs/homeblog/utils_file.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-01-27 05:44 +0100

1""" 

2@file 

3@brief Reasonably inefficient functions about files. 

4""" 

5import hashlib 

6 

7 

8def checksum_md5(filename): 

9 """computes MD5 for a file 

10 @param filename filename 

11 @return string or None if there was an error 

12 """ 

13 fname = filename 

14 block_size = 0x10000 

15 

16 fd = open(fname, "rb") 

17 try: 

18 block = [fd.read(block_size)] 

19 while len(block[-1]) > 0: 

20 block.append(fd.read(block_size)) 

21 contents = block 

22 zero = hashlib.md5() 

23 i = 0 

24 for el in contents: 

25 i += 1 

26 zero.update(el) 

27 m = zero 

28 return m.hexdigest() 

29 finally: 

30 fd.close() 

31 return None