使用pbkdf2的SALT和HASH [英] SALT and HASH using pbkdf2

查看:170
本文介绍了使用pbkdf2的SALT和HASH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法从nodejs中的加密库创建一个加盐和哈希的密码:

I am using the following methods to create a salted and hashed password from the crypto lib in nodejs:

crypto.randomBytes(size, [callback])
crypto.pbkdf2(password, salt, iterations, keylen, callback)

对于randomBytes调用(创建SALT),我应该使用什么大小?我听说过128位盐,可能高达256位.看来此函数使用的字节大小为单位,所以我可以假设32位(256位)的大小就足够了吗?

For the randomBytes call (creating the SALT) what size should I use? I have heard 128-bit salts, maybe up to 256-bit. It looks like this function uses a size in bytes so can I assume a size of 32 (256 bits) is sufficient?

对于pbkdf2调用,键(keylen)的迭代次数是多少,长度是多少?

For the pbkdf2 call, what is a good number of iterations and what is a good length for the key (keylen)?

另外,对于存储,我已经看到了将盐,长度,迭代次数和derviedkey存储在同一列中的示例.我正在使用一个用::分隔4的示例,即:

Also, for storage I have seen examples of storing the salt, length, iterations and derviedkey in the same column. I am using an example which separates the 4 by ::, i.e.:

salt::derivedKey::keyLength::iterations

执行此操作,然后可以在::上分开以获取4个值,因此我可以基于提供的密码生成派生密钥,以查看其是否匹配.这是正确的存储方式吗?还是在合并这些值时我应该更具欺骗性"?

Doing this, I can then separate on :: to get the 4 values, so I can generate a derived key based on a provided password to see if it matches. Is this the correct way to store this? Or should I be a little more "deceptive" in combining these values?

推荐答案

1.随机字节大小:

盐的大小至少应与哈希函数的大小相同,因此对于sha256,您应至少使用32个字节. Node.js Crypto的pbkdf2使用SHA1,因此最小长度应为20个字节.但是,您最少应该使用 64位(8字节),如#3中所述. (来源: https://crackstation.net/hashing-security.htm ).

Salts should be at least the same size as your hash function, so for sha256 you should use at least 32 bytes. Node.js Crypto's pbkdf2 uses SHA1, so 20 bytes should be the minimum. However, the least you should use is 64 bits (8 bytes), as explained in #3. (Source: https://crackstation.net/hashing-security.htm).

2. PBKDF2迭代次数:

有关大量讨论,请参见此问题.我从中得出 10.000范围就足够了,而不会影响性能,但这取决于硬件/性能.

See this question for a great discussion. I took from it that 10.000 range is sufficient without impact performance, but this is hardware/performance dependant.

3. PBKDF2长度:

有关密钥长度的信息,请参见此其他讨论.该参数还是使用的哈希函数(在您的情况下为SHA-1),因此20字节是正确的值.由于 PBKDF2的标准建议至少 64位的盐,所以这很浪费生成小于输入内容的键,因此请至少使用 8个字节.不要使用大于20的输出长度,因为它不会提供额外的安全性,但是会使20的倍数的计算时间增加一倍.

See this other discussion about key lengths. The parameter is again the hashing function used, in your case SHA-1, so 20 bytes is the correct value. Since PBKDF2's Standard recommends salts of at least 64 bits, it's a waste to generate keys smaller than your input, so use at least 8 bytes. Do not use output length of greater than 20, as it provides no additional security, but doubles computation time for each multiple of 20.

4.如何存储变量:

在以上所有链接(尤其是第一个)中讨论过,盐应与密码一起保存(但从未在其他地方重复使用),通常是先将其附加到结果字符串(salt:hash)或另一个数据库列中.

Discussed in all the links above (especially the first), salts should be saved along passwords (but never reused elsewhere), usually by appending it first in the resulting string (salt:hash), or in another database column.

至于其他变量,它们的知识对于破坏安全性不是至关重要的(如 Kerckhoffs原理中所述,这样您就可以在任何地方安全地对其进行参数化了.用"::"分隔它们的方法很好,但是您正在保存更多信息.,因此您所需的只是 "salt::derivedKey::iterations" .

As far as other variables, their knowledge is not critical for breaching security (as stated in Kerckhoffs's Principle, so you could parametrize it safely anywhere. Your way of doing it by separating them with "::" is fine, but you are saving extra information. Crackstation's codes only save "algorithm:iterations:salt:hash", so in your case, "salt::derivedKey::iterations" is all you need.

这篇关于使用pbkdf2的SALT和HASH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆