Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
用于安全密码哈希的 Bcrypt 算法_创想鸟

用于安全密码哈希的 Bcrypt 算法

哈希是一种无法逆转的加密函数。它需要随机大小的输入来生成固定大小的值。这些固定大小的值称为哈希值, 加密函数称为哈希函数。散列具有一致和可预测的性质,这意味着相同的输入将始终产生相同的散列值。它还表现出雪崩效应,这意味着即使输入的微小变化也会导致哈希值截然不同,从而确保高安全性和不确定性。

用于安全密码哈希的 Bcrypt 算法

散列通常采用加盐散列,其中在散列之前将称为盐的唯一随机字符串添加到输入中,即使对于相同的输入,每个散列也是唯一的

加盐哈希主要用于密码哈希。其中一种算法是bcrypt 算法。

bcrypt 算法

bcrypt 算法基于 blowfish 加密算法。 bcrypt 为每个密码生成唯一的 salt(随机字符串),然后将 salt 与密码组合后再进行哈希处理。这使得 bcrypt 能够抵抗暴力攻击。

bcrypt 的工作原理

生成盐:
bcrypt 生成一个 16 字节长的随机盐,通常采用 base64 格式。

对给定字符串进行哈希处理:
盐与密码组合,生成的字符串通过 blowfish 加密算法传递。 bcrypt 应用由工作因子定义的多轮哈希。高轮数使其计算成本高昂,从而增强了其对暴力攻击的抵抗力。
工作因子,也称为成本,由对数值 2 定义。如果成本为 12,则意味着 2^12 轮。成本系数越高,生成哈希所需的时间就越长,这反过来又使攻击者更难暴力破解密码。

bcrypt 哈希的格式和长度:

 $2y$12$odwbfokg9vtk/baarxkkl.9q8khxheysqpli/gsnpmzswqcajb.gs

给定的字符串包含:

$2y$:bcrypt 版本12 是成本因子(2^12 轮)接下来的22个字符(odwbfokg9vtk/baarxkkl。)是base64编码的盐其余字符是密码和盐的 base64 编码哈希。

pythonbcrypt算法的实现bcrypt算法

所需的依赖项

import hashlibimport osimport base64

类初始化

class bcrypt:    def __init__(self, rounds=12, salt_length=22):        self.rounds = rounds        self.salt_length = salt_length

bcrypt 类封装了哈希和验证密码的功能

参数:

生成盐

def generate_salt(self, salt_length=none):        if salt_length is none:            salt_length = self.salt_length        return base64.b64encode(os.urandom(salt_length)).decode('utf-8')[:salt_length]

函数generate_salt创建一个随机盐,它将是一个唯一的值,将被添加到密码中,以确保即使相同的密码也会产生不同的哈希值。

哈希密码

def bcrypt_hash(self, password, salt, cost):    password_salt = f'{password}{salt}'    password_salt = password_salt.encode('utf-8')    hashed_password_salt = hashlib.sha256(password_salt).hexdigest()    for _ in range(2**cost):        hashed_password_salt = hashlib.sha256(hashed_password_salt.encode('utf-8')).hexdigest()    return hashed_password_saltdef hash_password(self, password, salt_length=none, cost=none):    if salt_length is none:        salt_length = self.salt_length    if cost is none:        cost = self.rounds    salt = self.generate_salt(salt_length)    hashed_password = self.bcrypt_hash(password, salt, cost)    return f'{cost}${salt}${hashed_password}'

函数bcrypt_hash使用提供的盐和成本因子安全地散列密码。

和函数 hash_password 使用随机盐为给定密码生成安全哈希。

代码:

import hashlibimport osimport base64class bcrypt:    def __init__(self, rounds=12, salt_length=22):        self.rounds = rounds        self.salt_length = salt_length    def generate_salt(self, salt_length=none):        if salt_length is none:            salt_length = self.salt_length        return base64.b64encode(os.urandom(salt_length)).decode('utf-8')[:salt_length]    def bcrypt_hash(self, password, salt, cost):        password_salt = f'{password}{salt}'        password_salt = password_salt.encode('utf-8')        hashed_password_salt = hashlib.sha256(password_salt).hexdigest()        for _ in range(2**cost):            hashed_password_salt = hashlib.sha256(hashed_password_salt.encode('utf-8')).hexdigest()        return hashed_password_salt    def hash_password(self, password, salt_length=none, cost=none):        if salt_length is none:            salt_length = self.salt_length        if cost is none:            cost = self.rounds        salt = self.generate_salt(salt_length)        hashed_password = self.bcrypt_hash(password, salt, cost)        return f'{cost}${salt}${hashed_password}'    def verify_password(self, password, hashed_password):        cost, salt, hashed_password = hashed_password.split('$')        cost = int(cost)        return hashed_password == self.bcrypt_hash(password, salt, cost)bcrypt = bcrypt()password = 'vinayak'hashed_password = bcrypt.hash_password(password)print('string :', password, ' bcrypt hash :', hashed_password)print('verify password :', bcrypt.verify_password(password, hashed_password))print('verify invalid password :', bcrypt.verify_password('vinayak1', hashed_password))

输出:

python test.pystring : vinayak  bcrypt hash : 12$FxJAsfQ2+7WuMj+ZGPAdFE$546a20a2ad890186ab661cb4969e8651a6f75eb5d4ffa0706ba4153414b65ea5verify password : Trueverify invalid password : False

以上就是用于安全密码哈希的 Bcrypt 算法的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1354716.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
PyTorch 中的 FashionMNIST
上一篇 2025年12月13日 18:45:37
在 PyTorch 中移动 MNIST
下一篇 2025年12月13日 18:45:55

相关推荐

发表回复

登录后才能评论
关注微信