md5_utils.py 1004 Bytes
Newer Older
qunfeng qiu's avatar
qunfeng qiu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Version: 1.0
@Python Version:3.6.6
@Author: ludq1
@Email: ludq1@chinaunicom.cn
@date: 2023/04/07 11:40:00
@Description:
"""

import hashlib


class Md5Utils:
    r"""
    md5 utils类,不依赖任何其他应用基础类
    """

    def md5_hash(self, file_path, read_byte_once=1024):
        r"""
        获取文件的md5哈希值
        """
        md5_1 = hashlib.md5()  # 创建一个md5算法对象
        with open(file_path, 'rb') as f:  # 打开一个文件,必须是'rb'模式打开
            while 1:
                data = f.read(read_byte_once)  # 由于是一个文件,每次只读取固定字节
                if data:  # 当读取内容不为空时对读取内容进行update
                    md5_1.update(data)
                else:  # 当整个文件读完之后停止update
                    break
        ret = md5_1.hexdigest()  # 获取这个文件的MD5值
        return ret