gpt4 book ai didi

encryption - 为什么我们使用 "salt"来保护我们的密码?

转载 作者:行者123 更新时间:2023-12-02 22:53:13 24 4
gpt4 key购买 nike

我正在阅读this tutorial ,我遇到了以下关于加密的讨论。最后写着

In the last line, we’ve hashed the salt with the password, yielding an encrypted password that is virtually impossible to crack

但在我看来,同时拥有encrypted_pa​​sswordsalt的黑客可以像我们使用salt<一样执行“彩虹”技巧。/.

那么,我哪里错了?

谢谢!

$ rails console
>> require 'digest'
>> def secure_hash(string)
>> Digest::SHA2.hexdigest(string)
>> end
=> nil
>> password = "secret"
=> "secret"
>> encrypted_password = secure_hash(password)
=> "2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b"
>> submitted_password = "secret"
=> "secret"
>> encrypted_password == secure_hash(submitted_password)
=> true

Here we’ve defined a function called secure_hash that uses a cryptographic hash function called SHA2, part of the SHA family of hash functions, which we include into Ruby through the digest library.7 It’s not important to know exactly how these hash functions work; for our purposes what’s important is that they are one-way: there is no computationally tractable way to discover that

2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b is the SHA2 hash of the string "secret".

If you think about it, though, we still have a problem: if an attacker ever got hold of the hashed passwords, he would still have a chance at discovering the originals. For example, he could guess that we used SHA2, and so write a program to compare a given hash to the hashed values of potential passwords:

>> hash = "2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b"
>> secure_hash("secede") == hash
=> false
>> secure_hash("second") == hash
=> false
>> secure_hash("secret") == hash
=> true

So our attacker has a match—bad news for any users with password "secret". This technique is known as a rainbow attack.

To foil a potential rainbow attack, we can use a salt, which is a different unique string for each user.8 One common way to (nearly) ensure uniqueness is to hash the current time (in UTC to be time zone–independent) along with the password, so that two users will have the same salt only if they are created at exactly the same time and have the same password. Let’s see how this works using the secure_hash function defined in the console above:

>> Time.now.utc
=> Fri Jan 29 18:11:27 UTC 2010
>> password = "secret"
=> "secret"
>> salt = secure_hash("#{Time.now.utc}--#{password}")
=> "d1a3eb8c9aab32ec19cfda810d2ab351873b5dca4e16e7f57b3c1932113314c8"
>> encrypted_password = secure_hash("#{salt}--#{password}")
=> "69a98a49b7fd103058639be84fb88c19c998c8ad3639cfc5deb458018561c847"

In the last line, we’ve hashed the salt with the password, yielding an encrypted password that is virtually impossible to crack. (For clarity, arguments to hashing functions are often separated with --.)

最佳答案

彩虹表的计算成本很高。如果没有盐,您可以构建一个可以重复使用的彩虹表,因为密码“password”将始终产生相同的哈希值(md5=5f4dcc3b5aa765d61d8327deb882cf99,sha1=5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8),因此很容易在密码数据库中识别。/p>

对于盐,您必须为遇到的每种盐计算彩虹表。一个足够大的盐,比如说 32 位(理想情况下是 128 位甚至更多),意味着你必须为每个想要破解的密码计算一个彩虹表,从而在很大程度上违背了它的目的。

关于encryption - 为什么我们使用 "salt"来保护我们的密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5252943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com