python hashlib模块

一、简介

hashlib摘要算法

二、作用

密码->密文,不可逆

比对文件

三、密码

1、普通摘要

import hashlib
my_md5 = hashlib.md5()
my_md5.update(bytes('tom', encoding='utf-8'))
ret = my_md5.hexdigest()
print(ret)

2、静态加盐

import hashlib
my_md5 = hashlib.md5(bytes('slat', encoding='utf-8'))   # 静态加盐
my_md5.update(bytes('tom', encoding='utf-8'))
ret = my_md5.hexdigest()
print(ret)

3、动态加盐

import random
import hashlib
a = random.choice(list(range(100)))
my_md5 = hashlib.md5(bytes('slat', encoding='utf-8') + bytes(a))   # 动态加盐
my_md5.update(bytes('tom', encoding='utf-8'))
ret = my_md5.hexdigest()
print(ret)

四、比对文件

文件的比对,不需要加盐,直接比对就可以,文件可以一段一段的比对

import hashlib
my_md5 = hashlib.md5()
my_md5.update(bytes('hello', encoding='utf-8'))
my_md5.update(bytes('world', encoding='utf-8'))
ret = my_md5.hexdigest()
print(ret)      # fc5e038d38a57032085441e7fe7010b0

my_md5 = hashlib.md5()
my_md5.update(bytes('helloworld', encoding='utf-8'))
ret = my_md5.hexdigest()
print(ret)  # fc5e038d38a57032085441e7fe7010b0
原文地址:https://www.cnblogs.com/wt7018/p/10957918.html