大家好,欢迎来到IT知识分享网。
✨前言:
🌟什么是hash?
在计算机科学中,“哈希” (Hash) 是一种算法,它接受输入(或者“消息”),并返回一个固定长度的字符串,这个字符串称为输入的 “哈希值” 或者 “消息摘要” (Message Digest)。哈希函数的设计目的是尽可能快地将任意长度的数据转换为固定长度的输出结果,通常输出的哈希值远小于输入数据。
🌟哈希有以下几个重要特性:
🌟哈希的应用场景:
🌟hashlib基本用法
⭐️计算字符串的MD5值
import hashlib # 创建md5对象 m = hashlib.md5() # 更新哈希对象以字符串参数 m.update(b"hello, world!") # 获取16进制格式的摘要 print(m.hexdigest()) # b10a8db164e0b7a99be72e3fe5
⭐️计算文件的SHA-256值
为了计算大文件的哈希值,建议以块的形式读取文件内容,逐步更新哈希对象。
import hashlib sha256 = hashlib.sha256() with open("example.txt", "rb") as f: # 一次读取并处理1024字节 for chunk in iter(lambda: f.read(1024), b""): sha256.update(chunk) print(sha256.hexdigest()) # 88573b5d4539c29ee60b1acfab2928ca5231c4448b6e8c3fca773e52794ab1e9
🌟常用的哈希算法
🌟hashlib校验文件一致性
#!/usr/bin/env python # coding=utf-8 """ # @Time : 2024/5/8 # @Author : Summer # @File : # @describe: """ import hashlib def calculate_hash(file_path, algorithm=hashlib.sha256): """ 计算文件的哈希值。 :param file_path: 文件路径字符串。 :param algorithm: 要使用的哈希算法,默认为sha256。 :return: 文件内容的哈希值的十六进制字符串。 """ hash_obj = algorithm() with open(file_path, 'rb') as file: while chunk := file.read(4096): # 以4096字节为单位读取文件 hash_obj.update(chunk) return hash_obj.hexdigest() def check_file_consistency(file1_path, file2_path, algorithm=hashlib.sha256): """ 检查两个文件是否一致。 :param file1_path: 第一个文件的路径。 :param file2_path: 第二个文件的路径。 :param algorithm: 要使用的哈希算法,默认为sha256。 :return: 如果两个文件一致返回True,否则返回False。 """ file1_hash = calculate_hash(file1_path, algorithm) file2_hash = calculate_hash(file2_path, algorithm) # 如果哈希值相同,那么文件就一致 return file1_hash == file2_hash # 示例:检查两个文件是否一致 file1 = "D:\\PycharmProjects\\debug_test\\file1.txt" file2 = "D:\\PycharmProjects\\debug_test\\file2.txt" if check_file_consistency(file1, file2): print("两个文件一致。") else: print("两个文件不一致。")
🌟使用hashlib和os库对密码进行哈希处理(带盐)
使用哈希保护密码的步骤:
#!/usr/bin/env python # coding=utf-8 """ # @Time : 2024/5/8 # @Author : Summer # @File : # @describe: """ import hashlib import os def hash_password(password, salt=None): if salt is None: # 生成随机盐。这里使用16字节的随机盐。 salt = os.urandom(16) # 将密码和盐拼接并进行哈希处理 pwd_hash = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, ) # 返回盐和哈希值。注意这里的盐和哈希值都需要存储在数据库中。 return salt, pwd_hash def verify_password(stored_salt, stored_hash, user_password): # 使用存储的盐和输入的密码生成哈希值 salt, new_hash = hash_password(user_password, stored_salt) # 比较新的哈希值和存储的哈希值 return new_hash == stored_hash # 示例:哈希密码和验证密码例子 password = "my_super_secure_password" salt, pwd_hash = hash_password(password) print("盐:", salt) print("哈希值:", pwd_hash) # 验证密码 assert verify_password(salt, pwd_hash, password)
🌟 HMAC的加密方式
⭐️HMAC的加密方式
HMAC加密实际上不是“加密”处理,而是一种基于哈希的消息认证码。它不涉及传统的加密和解密过程,而是以保证消息完整性和验证身份为目的。
⭐️使用HMAC进行消息验证的过程包括以下几个步骤:
#!/usr/bin/env python # coding=utf-8 """ # @Time : 2024/5/8 # @Author : Summer # @File : # @describe: """ import hmac import hashlib # 密钥(在实际应用中,密钥应该是保密的) key = b"secret_key" # 待认证的消息 message = b"Hello, HMAC!" # 使用HMAC和SHA-256创建一个新的消息摘要对象 h = hmac.new(key, message, hashlib.sha256) # 获取HMAC摘要 hm = h.hexdigest() print("HMAC摘要:", hm) # 假设这是接收方计算的HMAC进行验证 h2 = hmac.new(key, message, hashlib.sha256) hm2 = h2.hexdigest() # 比较HMAC以验证消息 if hmac.compare_digest(hm, hm2): print("消息验证成功,完整且未被篡改。") else: print("消息验证失败。")
⚠️注意事项
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/115392.html