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.pswp.cn/news/392918.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392918.shtml
英文地址,請注明出處:http://en.pswp.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…