#!/usr/bin/python
-- coding: UTF-8 --
!python3.9.10
@Time : 2022/4/6 16:06
@Author : 妙玄
@Site :
@File : hash.py
什么是哈希
一类算法,该算法接收传入的内容经过运算得到一串哈希值
哈希值的特点:
(1)传入的值一样,得到的值一定一样
(2)不能由哈希值反解成内容————>把密码做成哈希值,不应该在网络传输明文密码
(3)只要使用的哈希算法不变,无论检验的内容有多大,得到的哈希值长度是固定的
用途:
(1)特点2用于明文密码加密:得到哈希字符串
(2)特点1,3用于文件完整性校验
如何使用:
import hashlib
haxi = hashlib.md5() # 得到一个哈希工厂
# 往哈希工厂传入内容, 接收的是bytes类型文件
haxi.update('hello'.encode('utf-8'))
# haxi.update('world'.encode('utf-8'))
# 得到哈希值
res = haxi.hexdigest() # 得到的是“helloworld”
print(res)
# 检验特点一
haxi1 = hashlib.md5('he'.encode('utf-8')) # 得到一个哈希工厂
# 往哈希工厂传入内容
haxi1.update('llo'.encode('utf-8'))
haxi1.update('w'.encode('utf-8'))
haxi1.update('or'.encode('utf-8'))
haxi1.update('ld'.encode('utf-8'))
res1 = haxi1.hexdigest()
print(res)
# 模拟撞库
# import hashlib
haxi_pwd = '5d41402abc4b2a76b9719d911017c592'
password = [
'hello',
'holle',
'hlole',
'hlloe',
'heoll'
]
dic = {}
for p in password:
result = hashlib.md5(p.encode('utf-8'))
dic[p] = result.hexdigest()
for k, v in dic.items():
if v == haxi_pwd:
print('撞库成功, 明文密码是:%s' % k)
# 提升撞库的成本:密码加盐
# 在密码的任意一处位置加入其他任意字符,使得密码复杂度提升
passwords = ['hello']
# 加盐后
passwords1 = [
'天王hello盖地虎',
'天王h盖地ello虎',
'天王hell地o盖虎',
]