python加密方法

1.签名加密算法

# -*- coding: utf-8 -*-
import hashlib

password_map = {"e10adc3949ba59abbe56e057f20f883e": "123456"}   # 哈希值与明文映射表

password = "123456" # 待加密明文

# 加密明文
_md5 = hashlib.md5()
_md5.update(password.encode("utf-8"))
p_md5 = _md5.hexdigest()
print(f"md5加密后的值为:{p_md5},通过md5映射表暴力破解出加密后的密码明文为:{password_map.get(p_md5)}")

2.加盐哈希加密算法

# -*- coding: utf-8 -*-
import hashlib
import random
import string


def generate_salt_hash(s: str):
    # 随机生成长度为4的盐值
    salt = "".join([random.choice(string.ascii_lowercase) for i in range(4)])

    # 拼接原始密码和盐值
    s = s + salt

    # 对加入盐值的字符串加密
    _md5 = hashlib.md5()
    _md5.update(s.encode("utf-8"))
    return salt + "$" + _md5.hexdigest()    # 返回 盐值+哈希值 的字符串


# 原始密码
password = "_rE7QWtl@ETWfMq5Fb1C_"

p_md5_1 = generate_salt_hash(password)
p_md5_2 = generate_salt_hash(password)
print(p_md5_1)
print(p_md5_2)

3.flask加密

from werkzeug.security import generate_password_hash, check_password_hash

# 明文密码
password = "_rE7QWtlETW#fMq5Fb1C_"

# 生成加密哈希值
p_hash = generate_password_hash(password)
print(p_hash)

# 验证密码
ret = check_password_hash(p_hash, password)
print(ret)

ret = check_password_hash(p_hash, "afdasfsda")
print(ret)


#################################################################################
from werkzeug.security import generate_password_hash, check_password_hash

# 明文密码
password = "_rE7QWtl@ETW#fMq5Fb1C_"
print(type(password))
data = {'password': 'pbkdf2:sha256:50000$gCrtclLT$a3c521c496fb6695f2f016bd3b4bf7b77ec5feacee45a2f031394bf9ee5092f7'}
print(data)
print(type(data))

ret = check_password_hash(data["password"],password)

print(ret)


你可能感兴趣的:(python,python,哈希算法,flask)