Refactor hashing operations

Extract common hashing operations into functions
Place new functions in new file digest.py
Update code to use the new functions