欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

常用方法 

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()) # 轉化為16進制打印md5值

結果

<md5 HASH object @ 0x000001C67C71C8A0>
e10adc3949ba59abbe56e057f20f883e

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

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())

結果

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)

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

相關文章

  • Opencv求取連通區(qū)域重心實例

    Opencv求取連通區(qū)域重心實例

    這篇文章主要介紹了Opencv求取連通區(qū)域重心實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python+PyQT5實現(xiàn)手繪圖片生成器

    Python+PyQT5實現(xiàn)手繪圖片生成器

    這篇文章主要介紹了利用Python PyQT5制作一個手繪圖片生成器,可以將導入的彩色圖片通過python分析光源、灰度等操作生成手繪圖片。感興趣的可以跟隨小編一起了解一下
    2022-02-02
  • python之當你發(fā)現(xiàn)QTimer不能用時的解決方法

    python之當你發(fā)現(xiàn)QTimer不能用時的解決方法

    今天小編就為大家分享一篇python之當你發(fā)現(xiàn)QTimer不能用時的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python實現(xiàn)員工管理系統(tǒng)

    python實現(xiàn)員工管理系統(tǒng)

    這篇文章主要介紹了python實現(xiàn)員工管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python中tqdm的使用和例子

    Python中tqdm的使用和例子

    Tqdm是一個快速,可擴展的Python進度條,可以在 Python 長循環(huán)中添加一個進度提示信息,用戶只需要封裝任意的迭代器tqdm(iterator),下面這篇文章主要給大家介紹了關于Python中tqdm的使用和例子的相關資料,需要的朋友可以參考下
    2022-09-09
  • python庫h5py入門詳解

    python庫h5py入門詳解

    本文只是簡單的對h5py庫的基本創(chuàng)建文件,數(shù)據(jù)集和讀取數(shù)據(jù)的方式進行介紹,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python告訴你木馬程序的鍵盤記錄原理

    Python告訴你木馬程序的鍵盤記錄原理

    今天小編就為大家分享一篇關于Python告訴你木馬程序的鍵盤記錄原理,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Python文件操作之合并文本文件內容示例代碼

    Python文件操作之合并文本文件內容示例代碼

    眾所周知Python文件處理操作方便快捷,下面這篇文章主要給大家介紹了關于Python文件操作之合并文本文件內容的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-09-09
  • 基于Python的socket庫實現(xiàn)通信功能的示例代碼

    基于Python的socket庫實現(xiàn)通信功能的示例代碼

    本文主要給大家介紹了如何使用python的socket庫實現(xiàn)通信功能,這里簡單的給每個客戶端增加一個不重復的uid,客戶端之間可以根據(jù)這個uid選擇進行廣播通信,感興趣的小伙伴快來看看吧
    2023-08-08
  • OpenCV圖像縮放之cv.resize()函數(shù)詳解

    OpenCV圖像縮放之cv.resize()函數(shù)詳解

    resize函數(shù)opencv中專門用來調整圖像大小的函數(shù),下面這篇文章主要給大家介紹了關于OpenCV圖像縮放之cv.resize()函數(shù)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09

最新評論