Python中做Hash加密

Hash中的salt

在密码学中,salt是随机数据,作为单向函数的额外输入,用于加密数据,如密码。
在这里插入图片描述
散列:散列将任何数量的数据转换为一个固定长度的散列,不能被逆转。

BCrypt算法

# pip install bcrypt

import bcrypt
  
# Declaring our password
password = b'wwsPassword'
  
# Adding the salt to password
salt = bcrypt.gensalt()
# Hashing the password
hashed = bcrypt.hashpw(password, salt)
  
# printing the salt
print("Salt :")
print(salt)
  
# printing the hashed
print("Hashed")
print(hashed)

'''
Salt :
b'$2b$12$keztAtu66dUoC3GwApT9aO'
Hashed
b'$2b$12$keztAtu66dUoC3GwApT9aOva4AGMuqnSfuwHhBT9/fvpi08gWCPd2'
'''

hashlib模块

Python hashlib模块是一个方便对消息进行散列的接口。它包含许多方法,可以处理将任何原始信息散列成加密格式。这个模块的主要目的是在一个字符串上使用哈希函数,并对其进行加密,这样就很难解密了。哈希库用于创建一个哈希表。散列表是一种数据结构,旨在通过一组条目进行搜索,每个条目都由一个唯一的密钥来识别。

#pip install hashlib
import hashlib
  
# Declaring Password
password = 'wwsPassword'
# adding 5gz as password
salt = "5gz"
  
# Adding salt at the last of the password
dataBase_password = password+salt
# Encoding the password
hashed = hashlib.md5(dataBase_password.encode())
  
# Printing the Hash
print(hashed.hexdigest())
'''
374731d38a6b21d806beebc37a3b8032
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值