简体   繁体   English

为什么要散列(不使用盐)随机数?

[英]Why hash (without using a salt) random numbers?

The following snippet of code is in php but I've seen developers of other web programming languages hash random numbers. 以下代码段位于php中,但我已经看到其他Web编程语言的开发人员对随机数进行哈希处理。

if ($loginValid=='yes') {
    $token = sha256(generateRandomNumber());
    $_SESSION["token"] = $token;
    $_SESSION["userid"] = $id;
    setcookie("authenticationtoken", $token, ...)
}

sha256 is a cryptographic hash function. sha256是加密哈希函数。 Since it does not use a salt, whatever input you give it, the same output comes out. 由于它不使用盐,因此无论您输入什么盐,都会产生相同的输出。 Try it out: 试试看:

http://www.xorbin.com/tools/sha256-hash-calculator http://www.xorbin.com/tools/sha256-hash-calculator

In what context is hashing (without using a salt) a random number considered good practice? 在什么情况下,哈希(不使用盐)是一个好的习惯?

My guess is that it's just a simple way of getting a hash of a known size, independently of the size that generateRandomNumber() returns. 我的猜测是,这只是一种获取已知大小的哈希的简单方法,而与generateRandomNumber()返回的大小无关。 Obviously if generateRandomNumber() has a small range, that will be reflected in the number of distinct hashes returned too. 显然,如果generateRandomNumber()的范围很小,这也将反映在返回的不同哈希数中。

Assuming that GenerateRandomNumber does not have a random seed, ie it is a truly cryptographically secure random number, a salt won't be required. 假设GenerateRandomNumber没有随机种子,即它是真正的密码安全随机数,则不需要添加盐。 Remember that the purpose of a salt is to ensure that if the same input is used to a hash function, it will return two different results. 请记住,盐的目的是确保如果将相同的输入用于哈希函数,它将返回两个不同的结果。

If token simply needs to be a secret that is passed to the client, then salting this hash won't really help. 如果令牌只是需要传递给客户端的秘密,那么对哈希值加盐实际上并没有帮助。

For reference, you would use a salt on a password hash. 作为参考,您可以在密码哈希上使用盐。 Rather than storing the user password, you store its hash so that if you database is penetrated no one can steal a user's password. 而不是存储用户密码,而是存储其哈希值,这样,如果您的数据库被渗透,则没人可以窃取用户密码。 However, you do not want people who use the same password to get the same hash, so you add a salt to the password before it is hashed to ensure that even if two users have the same password, they should get a different hash. 但是,您不希望使用相同密码的人获得相同的哈希值,因此您在对哈希值进行哈希运算之前先向其添加盐,以确保即使两个用户使用相同的密码,他们也应获得不同的哈希值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM