Shiros Sha256Hash和替代算法

时间:2015-09-04 15:05:29

标签: shiro hmac sha256 pbkdf2

来自Apache Shiro的Sha256Hash是基于像PBKDF2WithHmacSHA256这样的通用规范吗?

以下示例证明,Shiros Sha256Hash不会创建有效的PBKDF2WithHmacSHA256哈希值。

public static byte[] getEncryptedPassword(
    String password,
    byte[] salt,
    int iterations,
    int derivedKeyLength
) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeySpec keySpec = new PBEKeySpec(
        password.toCharArray(),
        salt,
        iterations,
        derivedKeyLength * 8
    );
    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    return f.generateSecret(keySpec).getEncoded();
}

@Test
public void testHashing(){
    byte[] salt = new SecureRandomNumberGenerator().nextBytes().getBytes();
    byte[] hash1 = new Sha256Hash("1234", salt, 1024).getBytes();
    byte[] hash2 = getEncryptedPassword("1234", salt, 1024, 32);
    assertTrue(hash1.equals(hash2));
}

有没有一种常用的方法可以使用shiro使用PBKDF2WithHmacSHA256,还是我必须实现自己的CredentialMatcher?

1 个答案:

答案 0 :(得分:1)

根据Shiro user list on nabble否,Shiro不提供PBKDF2(或BCrypt或SCrypt)。

请注意Java 8 does have PBKDF2-HMAC-SHA-512 available now为PBKDF2WithHmacSHA512 - 请改用它。特别是SHA-512具有64位操作,可以降低基于GPU的攻击者的优势。使用比1024更多的迭代 - 看看你的系统可以在负载下舒适地处理!

相关问题