Note that there are some explanatory texts on larger screens.

plurals
  1. POSecretKeyFactory.getInstance("PBKDF2WithHmacSHA512") throws NoSuchAlgorithmException
    primarykey
    data
    text
    <p>After a little bit of research and some work I finally was able to hash salt the password now there is a question which is on my mind I have used the SHA1 method and I would like to try to use the SHA512 because I was told it's better (more secure) so the following is my code its a little bit all over the place but I think its comprehensible so:</p> <pre><code>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 &gt; 0) return String.format("%0" + paddingLength + "d", 0) + hex; else return hex; } } </code></pre> <p>So that's my code, however, I have not been able to make that SHA512 and I have already tried <code>public static final String algorithm = "PBKDF2WithHmacSHA512"</code> but that doesn't seem to be the right string for the algorithm since it throws the no such algorithm exception.</p> <p>I also welcome any changes that would make the code better.</p> <p>as stated above! relevant few line(s) of code</p> <blockquote> <blockquote> <blockquote> <blockquote> <blockquote> <p>public static final String algorithm = "PBKDF2WithHmacSHA512"&lt;&lt;&lt;&lt;&lt;</p> </blockquote> </blockquote> </blockquote> </blockquote> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload