4G_module/tools/utils/python3/Crypto/library/hash.py

24 lines
847 B
Python
Raw Normal View History

2024-09-02 17:54:29 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
from Naked.toolshed.file import FileReader
# ------------------------------------------------------------------------------
# PUBLIC
# ------------------------------------------------------------------------------
def generate_hash(filepath):
"""Public function that reads a local file and generates a SHA256 hash digest for it"""
fr = FileReader(filepath)
data = fr.read_bin()
return _calculate_sha256(data)
# ------------------------------------------------------------------------------
# PRIVATE
# ------------------------------------------------------------------------------
def _calculate_sha256(binary_string):
"""Private function that calculates a SHA256 hash digest for a binary string argument"""
return hashlib.sha256(binary_string).hexdigest()