devise tree_Devise如何确保您的Rails应用密码安全

devise tree

by Tiago Alves

由蒂亚戈·阿尔维斯(Tiago Alves)

Devise如何确保您的Rails应用密码安全 (How Devise keeps your Rails app passwords safe)

Devise is an incredible authentication solution for Rails with more than 40 million downloads. However, since it abstracts most of the cryptographic operations, it’s not always easy to understand what’s happening behind the scenes.

Devise是Rails令人难以置信的身份验证解决方案, 下载量超过4000万 。 但是,由于它抽象了大多数密码操作,因此了解幕后发生的事情并不总是那么容易。

One of those abstractions culminates in the persistence of an encrypted_password directly on the database. So I’ve always been curious about what it actually represents. Here’s an example:

这些抽象之一最终导致直接在数据库上encrypted_password了password_password。 因此,我一直对它真正代表什么感到好奇。 这是一个例子:

$2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO

$2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO

But what does that gibberish mean?

但是那胡言乱语是什么意思呢?

Devise uses Bcrypt to securely store information. On its website it mentions that it uses “OpenBSD bcrypt() password hashing algorithm, allowing you to easily store a secure hash of your users’ passwords”. But what exactly is this hash? How does it work and how does it keep stored passwords safe?

Devise使用Bcrypt安全地存储信息。 它在其网站上提到它使用“ OpenBSD bcrypt()密码哈希算法,使您可以轻松地存储用户密码的安全哈希 ”。 但是这个哈希到底是什么? 它是如何工作的以及如何确保存储的密码安全?

That’s what I want to show you today.

那就是我今天想告诉你的。

Let’s work backwards — from the stored hash on your database to the encryption and decryption process.

让我们倒退一下-从数据库上存储的哈希到加密和解密过程。

That hash $2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO is actually comprised of several components:

哈希$2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO实际上由几个组成部分组成:

  • Bcrypt version (2a) - the version of the bcrypt() algorithm used to produce this hash (stored after the first $ sign)

    Bcrypt版本 ( 2a )-用于产生此哈希值的bcrypt()算法的版本(存储在第一个$符号之后)

  • Cost (11) - the cost factor used to create the hash (stored after the second $ sign)

    成本 ( 11 )-用于创建哈希的成本因子(存储在第二个$符号之后)

  • Salt ($2a$11$yMMbLgN9uY6J3LhorfU9iu) - a random string that when combined with your password makes it unique (first 29 characters)

    Salt ( $2a$11$yMMbLgN9uY6J3LhorfU9iu )-一个随机字符串,与您的密码结合使用时会使其唯一(前29个字符)

  • Checksum (LAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO) - the actual hash portion of the stored encrypted_password (remaining string after the 29 chars)

    校验和 ( LAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO ) -所存储的实际的哈希部encrypted_password (29个字符之后剩余字符串)

Let’s explore the last 3 parameters:

让我们探索最后三个参数:

  • When using Devise, the Cost value is set by a class variable called stretches and the default value is 11. It specifies the number of times the password is hashed. (On your devise.rb initializer, you can configure this to a lower value for the test environment to make your test suite run faster.) *

    使用Devise时, Cost值由一个称为stretchs的类变量设置,默认值为11 。 它指定密码被哈希的次数。 ( 在devise.rb初始化程序上 ,可以将其配置为测试环境的较低值,以使测试套件运行更快。 )

  • The salt is the random string used to combine with the original password. This is what makes the same password have different values when stored encrypted. (See more below about why that matters and what are Rainbow Table Attacks.) **

    是用于与原始密码组合的随机字符串。 这就是在加密存储时使同一密码具有不同值的原因。 ( 请参阅下面的更多内容,以了解为何如此重要以及什么是Rainbow Table Attack 。)**

  • The checksum is the actual generated hash of the password after being combined with the random salt.

    校验和是密码与随机盐组合后实际生成的哈希值。

When a user registers on your app, they must set a password. Before this password is stored in the database, a random salt is generated via BCrypt::Engine.generate_salt(cost) by taking into account the cost factor previously mentioned. (Note: if the pepper class variable value is set it will append its value to the password before salting it.)

用户在您的应用上注册时,必须设置密码。 在将此密码存储在数据库中之前,考虑到前面提到的成本因素,会通过BCrypt :: Engine.generate_salt(cost)生成随机盐。 (注意:如果设置了pepper 类变量值 ,它将在盐腌之前将其值附加到密码上 。)

With that salt (ex. $2a$11$yMMbLgN9uY6J3LhorfU9iu, which includes the cost factor) it will call BCrypt::Engine.hash_secret(password, salt) that computes the final hash to be stored using the generated salt and the password selected by the user. This final hash (for example, $2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO) will in turn be stored in the encrypted_password column of the database.

使用该盐(例如$2a$11$yMMbLgN9uY6J3LhorfU9iu ,其中包括成本因素),它将调用BCrypt :: Engine.hash_secret(password,salt) ,使用生成的盐和由密码选择的密码来计算要存储的最终哈希用户。 此最终散列(例如, $2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO )将被依次存储在encrypted_password数据库的列。

But if this hash is nonreversible and the salt is randomly generated on the BCrypt::Password.create call by BCrypt::Engine.generate_salt(cost), how can it be used to sign in the user?

但是,如果此哈希不可逆并且盐在BCrypt::Engine.generate_salt(cost)BCrypt::Password.create调用中随机生成, 如何将其用于登录用户?

That’s where those different hash components are useful. After finding the record that matches the email supplied by the user to sign in, the encrypted password is retrieved and broken down into the different components mentioned above (Bcrypt version, Cost, Salt and Checksum).

那是那些不同的哈希组件有用的地方。 找到与用户提供的用于登录的电子邮件匹配的记录后,将检索加密的密码并将其分解为上述不同的组成部分( Bcrypt版本CostSaltChecksum )。

After this initial preparation, here’s what happens next:

初步准备之后,接下来将发生以下情况:

  1. Fetch the input password (1234)

    获取输入的密码 ( 1234 )

  2. Fetch the salt of the stored password ($2a$11$yMMbLgN9uY6J3LhorfU9iu)

    获取存储的密码的 ( $2a$11$yMMbLgN9uY6J3LhorfU9iu )

  3. Generate the hash from the password and salt using the same bcrypt version and cost factor (BCrypt::Engine.hash_secret(“1234”, “$2a$11$yMMbLgN9uY6J3LhorfU9iu”))

    使用相同的bcrypt版本和成本因子( BCrypt::Engine.hash_secret(“1234”, “$2a$11$yMMbLgN9uY6J3LhorfU9iu”)从密码和salt生成哈希

  4. Check if the stored hash is the same one as the computed on step 3 ($2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO)

    检查存储的哈希值是否与步骤3上计算的哈希值相同( $2a$11$yMMbLgN9uY6J3LhorfU9iuLAUwKxyy8w42ubeL4MWy7Fh8B.CH/yO )

And that’s how Devise stores passwords securely and protects you from a range of attacks even if your database is compromised.

这样一来,即使数据库受到威胁,Devise仍可以安全地存储密码并保护您免受一系列攻击。

Get in touch on Twitter @alvesjtiago and let me know if you found this article interesting! Thank you for reading.

在Twitter @alvesjtiago上保持联系,如果您发现本文有趣,请告诉我! 感谢您的阅读。

PS: I’m by no means a security or cryptography expert so please do reach out if you find something wrong. I’m hoping that by simplifying some of the concepts it will be easier to understand what’s happening.
PS:我绝不是安全或加密专家,所以如果发现错误,请务必与我们联系。 我希望通过简化一些概念,可以更轻松地了解正在发生的事情。

Thank you @filipepina, @ivobenedito, @jackveiga, @joao_mags and @pedrosmmoreira for the reviews and suggestions. This article is also available at http://blog.tiagoalves.me/how-does-devise-keep-your-passwords-safe.

感谢@filipepina , @ivobenedito , @jackveiga , @joao_mags和@pedrosmmoreira的评论和建议。 本文也可以从http://blog.tiagoalves.me/how-does-devise-keep-your-passwords-safe获得 。

More information about some of the topics.

有关某些主题的更多信息。

Cost factor *

成本因素*

  • Perils of the default bcrypt cost factor

    默认bcrypt成本因素的风险

  • Recommended number of rounds for bcrypt

    建议的bcrypt轮数

Rainbow Table Attacks **

彩虹桌攻击**

  • Rainbow table — Wikipedia

    彩虹桌—维基百科

  • What are rainbow tables and how are they used?

    什么是彩虹桌,如何使用?

翻译自: https://www.freecodecamp.org/news/how-does-devise-keep-your-passwords-safe-d367f6e816eb/

devise tree

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392918.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Exchange 2010无法安装问题解决方法

当你在活动目录(AD)森林中安装多台全局编录服务器(GC)之后,默认情况下你会发现在AD站点里面自动生成二条站点连接,从上面的截图可以看到目前在AD森林的Default-First-Site-Name(默认站点)里面有6台GC。 从上面的截图可以看到目前只有一台叫做Sh-Site1GC(全局编录服务器)是处于运…

android edittext 不滚动,EditText 设置可以垂直滑动但是不可输入

一、前言:android:id"id/edtInput"android:layout_width"match_parent"android:layout_height"60dp"android:background"drawable/round_theme_3_gray"android:gravity"top"android:hint"string/please_inp…

snmpd修改端口

http://blog.csdn.net/cau99/article/details/5077239 http://blog.csdn.net/gua___gua/article/details/48547701转载于:https://www.cnblogs.com/diyunpeng/p/6829592.html

leetcode LCP 19. 秋叶收藏集(dp)

小扣出去秋游,途中收集了一些红叶和黄叶,他利用这些叶子初步整理了一份秋叶收藏集 leaves, 字符串 leaves 仅包含小写字符 r 和 y, 其中字符 r 表示一片红叶,字符 y 表示一片黄叶。 出于美观整齐的考虑,小扣…

步进电机 步距角 编码器_我如何迈出了学习编码的第一步

步进电机 步距角 编码器A couple of months ago, I was chatting to a developer at work about how I’ve always wanted to learn to code but never tried.几个月前,我正在与一个开发人员聊天,讨论我一直想学习编码但从未尝试过的方法。 Coding alwa…

第五章:配置使用FastJson返回Json视图

fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件。它是目前json解析最快的开源组件没有之一!在这之前jaskJson是命名为快速操作json的工具,而当阿里巴巴的fastJson诞生后jaskjson就消声…

一加6android9玩飞车掉,解锁新速度:一加6T深度评测

解锁新速度:一加6T深度评测2019-11-02 14:28:595点赞2收藏4评论创作立场声明:我们只谈智能硬件,向改变生活的智能硬件Say“嗨”!作为安卓旗舰机成员,一加这个品牌在玩机一类的同学手里可是大放光彩,各种刷机…

设计模式(第十七式:迭代器模式)

概念:  迭代器模式:Provide a way to access the elements of an aggregarte object sequentiaally with exposing its underlying representation. 提供一种访问容器对象内每个元素的一种方式,并且不暴露对象的一些内部细节。实现&#xf…

探讨跨域请求资源的几种方式

[转自:http://www.cnblogs.com/dojo-lzz/p/4265637.html] 什么是跨域JSONPproxy代理corsxdr由于浏览器同源策略,凡是发送请求url的协议、域名、端口三者之间任意一与当前页面地址不同即为跨域。具体可以查看下表(来源) JSONP 这种…

算法训练营 重编码_编码训练营适合您吗?

算法训练营 重编码by Joanna Gaudyn乔安娜高登(Joanna Gaudyn) 编码训练营适合您吗? (Is a Coding Bootcamp something for you?) Coding bootcamps’ popularity is growing. It sounds like a perfect idea to fast-forward your career. But is it really some…

leetcode 771. 宝石与石头(set)

给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a…

用ntdsutil命令中的restore object 更新版本号

备份域控建立好后,备份域信息,用目录还 原模式,还原域信息,用ntdsutil命令,中的 restore ob ject 更新版本号 本文转自9pc9com博客,原文链接: http://blog.51cto.com/215363/783334 如需…

python处理excel文件(xls和xlsx)

一、xlrd和xlwt 使用之前需要需要先安装,windows上如果直接在cmd中运行python则需要先执行pip3 install xlrd和pip3 install xlwt,如果使用pycharm则需要在项目的解释器中安装这两个模块,File-Settings-Project:layout-Project Interpreter&a…

html块中的内容垂直居中,css如何设置行内元素与块级元素的内容垂直居中

首先我们先了解一下行内元素和块级元素行内元素(内联元素):没有自己的独立空间,它是依附于其他块级元素存在的,空间大小依附于内容多少。行内元素没有度、宽度、内外边距等属性。块级元素:占据独立的空间,具有宽度&…

Mina、Netty、Twisted一起学(五):整合protobuf

protobuf是谷歌的Protocol Buffers的简称,用于结构化数据和字节码之间互相转换(序列化、反序列化),一般应用于网络传输,可支持多种编程语言。protobuf怎样使用这里不再介绍,本文主要介绍在MINA、Netty、Twi…

leetcode 1. 两数之和(map)

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 示例: 给定 nums [2, 7, 11, 15], target …

Redis 3.0.1 安装和配置

一、下载,解压和编译Redis 12345# cd /tmp # wget http://download.redis.io/releases/redis-3.0.1.tar.gz # tar xzf redis-3.0.1.tar.gz # cd redis-3.0.1 # make二、下载、安装tclsh 测试编译: 1# make test得到如下错误信息: …

2021年南宁二中高考成绩查询,2021广西高考圆满结束,6月23日可查询成绩

6月8日下午,2021年高考统考圆满结束。今年广西参加高考统考考生人数40.05万余人,比2020年增加了2.2万人。我区预计6月23日可查询高考成绩,6月24日起可陆续填报志愿,我区的网上咨询会将于6月25日至27日举办。▲高考结束&#xff0c…

29 Python - 字符与编码

字符与编码 01 字符串本质 Python字符串相关概念 字符串 str 字节 bytes 字节数组 bytearray 电脑字符串存储机制 字符库:A、B每个字符有一个代码点如A是65 B为66,这种是方便人类读写的形式,但是最终需要存入计算机的CPU和内存&…

Linux 内存管理与系统架构设计

Linux 提供各种模式(比如,消息队列),但是最著名的是 POSIX 共享内存(shmem,shared memory)。 Linux provides a variety of schemes (such as message queues), but most notable is POSIX shar…