SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512") 抛出 NoSuchAlgorithmException

Fah*_*i95 6 java security algorithm sha

经过一点研究和一些工作后,我终于能够对密码进行哈希盐处理了,现在我脑子里有一个问题,我已经使用了 SHA1 方法,我想尝试使用 SHA512,因为有人告诉我它更好(更安全)所以以下是我的代码,它有点乱,但我认为它是可以理解的:

public class Safety
{
   //calling some parameters for possible later changes
   public static final String algorithm = "PBKDF2WithHmacSHA1";
   public static final int saltbytesize = 24;
   public static final int hashbytesize = 24;
   public static final int iterations = 1000;
   public static final int iIndex = 0;
   public static final int sIndex = 1;
   public static final int pbkIndex = 2;

   public static Users passwordHash(Users user) throws NoSuchAlgorithmException,
                                                       InvalidKeySpecException
   {
      SecureRandom sR = new SecureRandom();

      byte[] pws = new byte[saltbytesize];

      sR.nextBytes(pws);
      byte[] pwh = pbkdf2(user.getPassword().toCharArray(), pws, iterations, hashbytesize);

      user.setPassword(toHex(pwh));

      byte[] sas = new byte[saltbytesize];

      sR.nextBytes(sas);

      byte[] sah = pbkdf2(user.getsA().toCharArray(), sas, iterations, hashbytesize);

      user.setsA(toHex(sah));

      user.setUserhash(pws);

      user.setSahash(sas);

      return user;
   }

   public static boolean hashpassword(String username, String password, Users user)
   throws NoSuchAlgorithmException,
          InvalidKeySpecException
   {
      byte[] pws = user.getUserhash();

      byte[] pwh = pbkdf2(password.toCharArray(), pws, iterations, hashbytesize);

      String searcher = toHex(pwh) + username;

      String searched = user.getPassword() + user.getUsername();

      if (searcher.equals(searched))
      {
         return true;
      }
      return false;
   }

   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(algorithm);
      return skf.generateSecret(spec).getEncoded();
   }

   private static String toHex(byte[] array)
   {
      BigInteger bi = new BigInteger(1, array);

      String hex = bi.toString(16);

      int paddingLength = (array.length * 2) - hex.length();

      if (paddingLength > 0)
         return String.format("%0" + paddingLength + "d", 0) + hex;
      else
         return hex;
   }
}
Run Code Online (Sandbox Code Playgroud)

这就是我的代码,但是,我无法制作 SHA512 并且我已经尝试过,public static final String algorithm = "PBKDF2WithHmacSHA512"但这似乎不是该算法的正确字符串,因为它抛出了 no such algorithm 异常。

我也欢迎任何可以使代码变得更好的更改。

如上所述!相关的几行代码

公共静态最终字符串算法=“PBKDF2WithHmacSHA512”<<<<<

Jav*_*ier 6

这是不可能开箱即用的

OpenJDK 实现仅提供PBKDF2HmacSHA1Factory.java,其中对“HmacSHA1”摘要进行了编码。据我测试,Oracle JDK 在这个意义上没有什么不同。

您所要做的就是派生PBKDF2HmacSHA1Factory(来吧,它是开放的!)并向其构造函数添加一个参数。您可以避免创建自己的混乱Provider,只需初始化并使用您的工厂,如下所示:

PBKDF_SecretKeyFactory kf = new PBKDF_SecretKeyFactory("HmacSHA512");
KeySpec ks = new PBEKeySpec(password,salt,iterations,bitlen);
byte key[] = kf.engineGenerateSecret(ks).getEncoded();
Run Code Online (Sandbox Code Playgroud)

  • Java 8 现在提供 PBKDF2HmacSHA512 - https://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html (5认同)