August 2018
Intermediate to advanced
366 pages
10h 14m
English
The first thing the function does is open the file in binary mode. As all hash functions require bytes and we don't even know the content of the file, reading it in binary mode is the most convenient solution.
Then, it checks whether the requested hashing algorithm is available in hashlib. That's done through getattr by trying to grab hashlib.md5, hashlib.sha256, and so on. If the algorithm is not supported, it won't be a valid hashlib attribute (as it won't exist in the module) and will throw AttributeError. To make those easier to understand, they are trapped and a new ValueError is raised that states clearly that the algorithm is not supported.
Once the file is opened and the algorithm is verified, an empty hash gets created ...