
1、什么是哈希hash
# 该算法接受传入的内容,经过运算得到一串hash值# hash值的特点是:# 只要传入的内容一样,得到的hash值必然一样=====>要用明文传输密码文件完整性校验# 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码# 只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的
2、hash的用途
# 用途1:特点II用于密码密文传输与验证# 用途2:特点I、III用于文件完整性校验
3、如何用
import hashlibm = hashlib.sha256()m.update('hello'.encode('utf-8'))m.update('world'.encode('utf-8'))res = m.hexdigest()print(res) # 936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07afm = hashlib.sha256('he'.encode('utf-8'))m.update('llo'.encode('utf-8'))m.update('wor'.encode('utf-8'))m.update('ld'.encode('utf-8'))res = m.hexdigest()print(res) # 936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af# 只要内容一样,hash值都一样print(hashlib.sha256('helloworld'.encode('UTF-8')).hexdigest()) # 936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af
撞库的方式破解加密密码
import hashlibpasswd='a42d9861eaba373b6d32977eaff32f12c408fe9620d998a1009f8996e2693ec6' # xio0327 加密后的# 制作密码字段passwds = ['xio0327', '0327xio', 'x0327io', 'xi0327o', '03xio27']dic = {}for p in passwds: res = hashlib.sha256(p.encode('utf-8')) # 首先你得知道他是用什么算法加密的 dic[p] = res.hexdigest()# 模拟撞库得到密码for k,v in dic.items(): if v == passwd: print('撞库成功,密码是%s' % k) # 撞库成功,密码是xio0327 break
密码加盐
import hashlibm = hashlib.sha256()m.update('撒'.encode('utf-8'))m.update('xio'.encode('utf-8'))m.update('盐'.encode('utf-8'))m.update('0327'.encode('utf-8'))print(m.hexdigest())