gpt4 book ai didi

java - 使用 PBKDF2WithHmacSHA512 的密码保护在生产设置 (AWS) 上花费了大量的处理时间

转载 作者:行者123 更新时间:2023-12-01 11:18:53 25 4
gpt4 key购买 nike

我正在尝试使用以下步骤安全地存储我的用户密码

  1. 使用 PBKDF2WithHmacSHA512 进行加盐+散列+拉伸(stretch)
  2. 迭代 20,000 次

我的代码粘贴在下面

public class EncrypterClass
{
private static final int iterations = 20000;
private static final int saltLen = 64;
private static final int desiredKeyLen = 512;

private static String hash(String value, byte[] salt) throws Exception
{
if (value == null || value.length() == 0)
throw new IllegalArgumentException("Empty passwords are not supported.");
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
SecretKey key = f.generateSecret(new PBEKeySpec(value.toCharArray(), salt, iterations, desiredKeyLen));
return Base64.encodeBase64String(key.getEncoded());
}

public static class Password
{
public static String getSaltedPassword( String password ) throws Exception
{
byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen);
return Base64.encodeBase64String(salt) + "$" + hash(password, salt);
}

public static boolean match(String password, String saltedPassword) throws Exception
{
String[] saltAndPass = saltedPassword.split("\\$");
if (saltAndPass.length != 2)
{
return false;
}
String hashOfInput = hash(password, Base64.decodeBase64(saltAndPass[0]));
return hashOfInput.equals(saltAndPass[1]);
}
}
}

当我在本地计算机上测试它时,它工作正常。但在我的 AWS 设置流程中,需要进行某种耗时的处理(需要几分钟来对密码进行哈希处理)。目前,我的 AWS 设置是免费测试设置,具有以下资源:具有 1 个 vCPU 和 1GiB 的 EC2 t2.micro

有人可以指导一下可能出现的问题吗?

谢谢阿米特

最佳答案

这种盐 + 哈希算法,尤其是在迭代次数如此多的情况下,其设计速度很慢,而且是故意的:其目标正是使攻击者通过暴力攻击找到密码的计算难度变得非常大。在资源有限的虚拟机上,显然会更慢。

关于java - 使用 PBKDF2WithHmacSHA512 的密码保护在生产设置 (AWS) 上花费了大量的处理时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31491245/

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