Python 获取md5值(hashlib)
生活随笔
收集整理的這篇文章主要介紹了
Python 获取md5值(hashlib)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常用方法?
import hashlib# 創建MD5對象,可以直接傳入要加密的數據 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?
如果要被加密的數據太長,可以分段update, 結果是一樣的
import hashlibstr = '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 hashlibdef get_file_md5(file_name):"""計算文件的md5:param file_name::return:"""m = hashlib.md5() #創建md5對象with open(file_name,'rb') as fobj:while True:data = fobj.read(4096)if not data:breakm.update(data) #更新md5對象return m.hexdigest() #返回md5對象def get_str_md5(content):"""計算字符串md5:param content::return:"""m = hashlib.md5(content) #創建md5對象return m.hexdigest()?
某源碼封裝
class Files(Storage):@staticmethoddef 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@staticmethoddef 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)@staticmethoddef 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@staticmethoddef 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))@staticmethoddef 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:breakh.update(buf)return h.hexdigest()@staticmethoddef md5_file(filepath):return Files.hash_file(hashlib.md5, filepath)@staticmethoddef sha1_file(filepath):return Files.hash_file(hashlib.sha1, filepath)@staticmethoddef sha256_file(filepath):return Files.hash_file(hashlib.sha256, filepath)?
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Python 获取md5值(hashlib)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Centos7利用fpm制作rpm包(f
- 下一篇: Python删除文件、删除文件夹