最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?獲取md5值(hashlib)常用方法

 更新時間:2023年07月18日 11:34:37   作者:墨痕訴清風(fēng)  
這篇文章主要介紹了Python獲取md5值(hashlib)常用方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

常用方法 

import hashlib
# 創(chuàng)建MD5對象,可以直接傳入要加密的數(shù)據(jù)
m = hashlib.md5('123456'.encode(encoding='utf-8'))
# m = hashlib.md5(b'123456') 與上面等價
print(hashlib.md5('123456'.encode(encoding='utf-8')).hexdigest())
print(m) 
print(m.hexdigest()) # 轉(zhuǎn)化為16進(jìn)制打印md5值

結(jié)果

<md5 HASH object @ 0x000001C67C71C8A0>
e10adc3949ba59abbe56e057f20f883e

如果要被加密的數(shù)據(jù)太長,可以分段update, 結(jié)果是一樣的

import hashlib
str = 'This is a string.'
m = hashlib.md5()
m.update('This i'.encode('utf-8'))
m.update('s a string.'.encode('utf-8'))
print(m.hexdigest())

結(jié)果

13562b471182311b6eea8d241103e8f0

封裝成常用庫md5.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
def get_file_md5(file_name):
    """
    計算文件的md5
    :param file_name:
    :return:
    """
    m = hashlib.md5()   #創(chuàng)建md5對象
    with open(file_name,'rb') as fobj:
        while True:
            data = fobj.read(4096)
            if not data:
                break
            m.update(data)  #更新md5對象
    return m.hexdigest()    #返回md5對象
def get_str_md5(content):
    """
    計算字符串md5
    :param content:
    :return:
    """
    m = hashlib.md5(content) #創(chuàng)建md5對象
    return m.hexdigest()

某源碼封裝

class Files(Storage):
    @staticmethod
    def temp_put(content, path=None):
        """Store a temporary file or files.
        @param content: the content of this file
        @param path: directory path to store the file
        """
        fd, filepath = tempfile.mkstemp(
            prefix="upload_", dir=path or temppath()
        )
        if hasattr(content, "read"):
            chunk = content.read(1024)
            while chunk:
                os.write(fd, chunk)
                chunk = content.read(1024)
        else:
            os.write(fd, content)
        os.close(fd)
        return filepath
    @staticmethod
    def temp_named_put(content, filename, path=None):
        """Store a named temporary file.
        @param content: the content of this file
        @param filename: filename that the file should have
        @param path: directory path to store the file
        @return: full path to the temporary file
        """
        filename = Storage.get_filename_from_path(filename)
        #dirpath = tempfile.mkdtemp(dir=path or temppath())
        dirpath = temppath()
        Files.create(dirpath, filename, content)
        return os.path.join(dirpath, filename)
    @staticmethod
    def create(root, filename, content):
        if isinstance(root, (tuple, list)):
            root = os.path.join(*root)
        filepath = os.path.join(root, filename)
        with open(filepath, "wb") as f:
            if hasattr(content, "read"):
                chunk = content.read(1024 * 1024)
                while chunk:
                    f.write(chunk)
                    chunk = content.read(1024 * 1024)
            else:
                f.write(content)
        return filepath
    @staticmethod
    def copy(path_target, path_dest):
        """Copy a file. The destination may be a directory.
        @param path_target: The
        @param path_dest: path_dest
        @return: path to the file or directory
        """
        shutil.copy(src=path_target, dst=path_dest)
        return os.path.join(path_dest, os.path.basename(path_target))
    @staticmethod
    def hash_file(method, filepath):
        """Calculate a hash on a file by path.
        @param method: callable hashing method
        @param path: file path
        @return: computed hash string
        """
        f = open(filepath, "rb")
        h = method()
        while True:
            buf = f.read(1024 * 1024)
            if not buf:
                break
            h.update(buf)
        return h.hexdigest()
    @staticmethod
    def md5_file(filepath):
        return Files.hash_file(hashlib.md5, filepath)
    @staticmethod
    def sha1_file(filepath):
        return Files.hash_file(hashlib.sha1, filepath)
    @staticmethod
    def sha256_file(filepath):
        return Files.hash_file(hashlib.sha256, filepath)

到此這篇關(guān)于Python 獲取md5值(hashlib)的文章就介紹到這了,更多相關(guān)Python 獲取md5值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python3.10接入ChatGPT實現(xiàn)逐句回答流式返回

    Python3.10接入ChatGPT實現(xiàn)逐句回答流式返回

    這篇文章主為大家要介紹了Python3.10接入ChatGPT實現(xiàn)逐句回答流式返回示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • python中將\\uxxxx轉(zhuǎn)換為Unicode字符串的方法

    python中將\\uxxxx轉(zhuǎn)換為Unicode字符串的方法

    這篇文章主要介紹了python中將\\uxxxx轉(zhuǎn)換為Unicode字符串的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • .dcm格式文件軟件讀取及python處理詳解

    .dcm格式文件軟件讀取及python處理詳解

    今天小編就為大家分享一篇.dcm格式文件軟件讀取及python處理詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python數(shù)據(jù)處理的三個實用技巧分享

    Python數(shù)據(jù)處理的三個實用技巧分享

    數(shù)據(jù)處理無所不在,掌握常用技巧,事半功倍。這篇文章將使用Pandas開展數(shù)據(jù)處理分析,總結(jié)其中常用、好用的數(shù)據(jù)分析技巧,感興趣的可以學(xué)習(xí)一下
    2022-04-04
  • Python中如何將一個類方法變?yōu)槎鄠€方法

    Python中如何將一個類方法變?yōu)槎鄠€方法

    這篇文章主要介紹了Python中如何將一個類方法變?yōu)槎鄠€方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • hmac模塊生成加入了密鑰的消息摘要詳解

    hmac模塊生成加入了密鑰的消息摘要詳解

    這篇文章主要介紹了hmac模塊生成加入了密鑰的消息摘要詳解,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python實現(xiàn)人機(jī)中國象棋游戲

    Python實現(xiàn)人機(jī)中國象棋游戲

    中國象棋是一種古老的棋類游戲,大約有兩千年的歷史。本文將介紹如何通過Python中的Pygame模塊實現(xiàn)人機(jī)中國象棋游戲,感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • 詳解Python3中的迭代器和生成器及其區(qū)別

    詳解Python3中的迭代器和生成器及其區(qū)別

    本篇將介紹Python3中的迭代器與生成器,描述可迭代與迭代器關(guān)系,并實現(xiàn)自定義類的迭代器模式。非常具有實用價值,需要的朋友可以參考下
    2018-10-10
  • Python將圖片轉(zhuǎn)為漫畫風(fēng)格的示例

    Python將圖片轉(zhuǎn)為漫畫風(fēng)格的示例

    本文主要介紹了Python將圖片轉(zhuǎn)為漫畫風(fēng)格的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Python對列表排序的方法實例分析

    Python對列表排序的方法實例分析

    這篇文章主要介紹了Python對列表排序的方法,實例分析了Python列表排序函數(shù)的相關(guān)使用技巧,非常簡單實用,需要的朋友可以參考下
    2015-05-05

最新評論

苍南县| 丹巴县| 罗定市| 且末县| 肃南| 和静县| 梁河县| 田阳县| 德庆县| 宜州市| 日照市| 鲁山县| 常熟市| 五家渠市| 永春县| 新巴尔虎左旗| 侯马市| 且末县| 鲁山县| 阿拉善盟| 稷山县| 安多县| 竹溪县| 彭水| 汝阳县| 滦平县| 越西县| 镇赉县| 华容县| 盐池县| 抚顺市| 光泽县| 德昌县| 平凉市| 锡林郭勒盟| 来宾市| 阿图什市| 沈丘县| 龙陵县| 淮安市| 巴南区|