用于安全密码哈希的 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 hashlib import os import 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_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}'
函数bcrypt_hash使用提供的盐和成本因子安全地散列密码。
和函数 hash_password 使用随机盐为给定密码生成安全哈希。
代码:
import hashlib
import os
import base64
class 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.py string : vinayak bcrypt hash : 12$FxJAsfQ2+7WuMj+ZGPAdFE$546a20a2ad890186ab661cb4969e8651a6f75eb5d4ffa0706ba4153414b65ea5 verify password : True verify invalid password : False
今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
Win10系统怎么关闭磁盘修复 Win10关闭磁盘修复教程
- 上一篇
- Win10系统怎么关闭磁盘修复 Win10关闭磁盘修复教程
- 下一篇
- 在linux脚本中关系运算符
-
- 文章 · python教程 | 1小时前 | python · Python 类型提示 typing get_protocol_members Protocol
- Python get_protocol_members 怎么读取 Protocol 成员集合
- 487浏览 收藏
-
- 文章 · python教程 | 13小时前 | 文件处理 · python · Python TempFile 临时文件 SpooledTemporaryFile rollover
- Python SpooledTemporaryFile 怎么手动触发写入磁盘
- 307浏览 收藏
-
- 文章 · python教程 | 16小时前 |
- Python Path.info 缓存的文件类型信息什么时候会过期
- 438浏览 收藏
-
- 文章 · python教程 | 19小时前 |
- Python asyncio.Queue shutdown 后等待者会收到什么
- 124浏览 收藏
-
- 文章 · python教程 | 1天前 |
- Python asyncio.Barrier 等待任务被取消后会怎样
- 166浏览 收藏
-
- 文章 · python教程 | 1天前 |
- Python TaskGroup 怎么主动终止整组任务
- 242浏览 收藏
-
- 文章 · python教程 | 1天前 | SQLite · 数据一致性 · Python教程 · Python SQLite 数据库备份 sqlite3.Connection.backup
- Python sqlite3.Connection.backup 怎么在线复制数据库
- 264浏览 收藏
-
- 文章 · python教程 | 1天前 | python · Python zip zipfile zipfile.Path
- Python zipfile.Path 怎么像目录一样遍历压缩包
- 370浏览 收藏
-
- 文章 · python教程 | 1天前 | 内存优化 · Python教程 · Python 大数组 PickleBuffer pickle协议5
- Python PickleBuffer 怎么减少大数组复制
- 207浏览 收藏
-
- 文章 · python教程 | 1天前 | 标准库 · Python教程 · Python Traversable importlib.resources zipimport
- Python importlib.resources.files 怎么访问压缩包内资源
- 143浏览 收藏
-
- 文章 · python教程 | 1天前 | 标准库 · python · 进程管理 · Python subprocess.Popen pipesize
- Python subprocess.Popen pipesize 什么时候有效
- 187浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 347次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 409次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 410次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 369次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 192次使用
-
- Python sqlite3 Connection serialize 怎么导出数据库快照:备份窗口、内存占用与恢复校验
- 2026-08-26 501浏览
-
- Python监控网页状态:requests异常处理实战
- 2026-05-29 501浏览
-
- TensorFlow模型部署为API的TF Serving方法
- 2026-05-26 501浏览
-
- Python字符串编码转换:encode与decode详解
- 2026-05-16 501浏览
-
- TensorFlow裁剪无用算子方法详解
- 2026-05-15 501浏览
