在Java中具有HMAC的PBKDF2 [英] PBKDF2 with HMAC in Java

查看:159
本文介绍了在Java中具有HMAC的PBKDF2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个Java项目中,我必须确保保存在纯文本文件中的用户密码的机密性和完整性.

I am working on a Java project where I must ensure the confidentiality and integrity of users password saved in a plaintext file.

为此,我将仅在文件中写入密码的哈希值.更具体地说,我的意图是编写密码和随机盐的哈希,再加上随机盐本身,以避免使用虹彩表和查找表.我还想对PBKDF2使用密钥拉伸,以使哈希的计算在计算上变得昂贵. 最后,我想使用密钥哈希算法HMAC作为最后一层保护.

To do so, I will write only a hash of the password in the file. More specifically, my intention is to write the hash of the password and a random salt, plus the random salt itself, to avoid the use of rainbow and lookup tables. I also want to use key-stretching with PBKDF2, to make the computation of the hash computationally expensive. Finally, I would like to use a keyed hash algorithm, HMAC, for a final layer of protection.

我试图用Java代码实现我的想法,并且发现了上面介绍的一些操作示例:

I am trying to implement my thoughts in a Java code, and I have found some examples of the operations that I have presented above:

private static byte[] pbkdf2(char[] password, byte[] salt, int iterations, int bytes)
    throws NoSuchAlgorithmException, InvalidKeySpecException
{
    PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    return skf.generateSecret(spec).getEncoded();
}

我真正无法理解的是如何输入我的秘密密钥作为HMAC算法使用的密钥,因为它似乎不是该函数的输入.我已经阅读了Java文档,但是找不到解决我问题的方法.

The thing that I really cannot understand is how to input my secret key as the key used by the HMAC algorithm, as it doesn't seem an input to the function. I have looked through the Java documentation, but I cannot find a solution to my question.

在这一点上,我不确定我是否能正确理解加密机制的不同部分是如何工作的,所以我会接受有关该主题的任何帮助.

At this point, I am not really sure if I understood correctly how the different part of the encryption mechanism work, so I would accept any help on the topic.

推荐答案

我认为我感到困惑.您显然希望您的代码先应用PBKDF2,然后再应用HMAC-SHA-1.这不是它的工作方式:PBKDF2内部使用了HMAC-SHA-1.

I think I see the confusion. You're apparently expecting your code to apply PBKDF2 then HMAC-SHA-1. That's not how it works: HMAC-SHA-1 is used inside PBKDF2.

PBKDF2 的要旨是重复应用具有以下属性的功能:

The gist of PBKDF2 is to apply a function repeatedly which has the following properties:

  • 有两个参数;
  • 它返回一个固定大小的值;
  • 与伪随机函数几乎没有区别.

HMAC-SHA-1是这样的功能,并且是常见的选择. PBKDF2还有其他变体,它们使用HMAC-MD5,HMAC-SHA-256或其他功能(但是这些变体不在基本Java库中).

HMAC-SHA-1 is such a function, and a common choice. There are other variants of PBKDF2, using HMAC-MD5, HMAC-SHA-256, or other functions (but these variants aren't in the basic Java library).

PBKDF2接受两个数据输入(加上一些配置输入):密码和一个盐.如果要在计算中包括一个秘密值,请使用PBKDF2的输入:不要在此之上使用自定义方案(使用自己的密码是做错它的秘诀).将 pepper (所有帐户共有的秘密值)附加到盐(不同帐户之间的公有价值).

PBKDF2 takes two data inputs (plus some configuration inputs): the password, and a salt. If you want to include a secret value in the calculation, PBKDF2's input is the place for it: don't tack on a custom scheme on top of that (doing your own crypto is a recipe for doing it wrong). Append the pepper (secret value common to all accounts) to the salt (public value that varies between accounts).

请注意,胡椒粉的用途有限.仅当哈希值和Pepper秘密值存储在不同的位置时才有用-例如,如果哈希值在数据库中并且Pepper在不直接受到SQL注入攻击攻击的磁盘文件中.

Note that pepper is of limited usefulness. It's only useful if the hashes and the pepper secret value are stored in different places — for example, if the hashes are in a database and the pepper is in a disk file that is not directly vulnerable to SQL injection attacks.

这篇关于在Java中具有HMAC的PBKDF2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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