SHA1Hash { @Nonnull public static SHA1Hash saltedHash(@Nonnull final String value) { assert value != null; byte[] salt = null; try { final SecureRandom random = SecureRandom.getInstance(SHA1PRNG_ALGORITHM); salt = new byte[8]; random.nextBytes(salt); } catch (final NoSuchAlgorithmException e) { LOG.error("saltedHash: Serious failure: No algorithm found for salt generation use {}. Proceeding " + "without salting the input. {}", SHA1PRNG_ALGORITHM, e.getMessage()); } return doHash(value, salt); } private  SHA1Hash(@Nonnull final String hash, @Nonnull final String salt); @SuppressWarnings({"ConstantConditions", "UnusedDeclaration"}) @Deprecated private  SHA1Hash(); @Nonnull static SHA1Hash hash(@Nonnull final String value); @Nonnull static SHA1Hash saltedHash(@Nonnull final String value); @Nonnull static SHA1Hash saltedHash(@Nonnull final String value, @Nonnull final String salt); @Nonnull String getSalt(); boolean canEqual(@Nonnull final Object obj); @Override boolean equals(@Nullable final Object obj); @Override int hashCode(); @Nonnull @Override String toString();  }
@Test public void testSaltedHash() { LOG.info("testSaltedHash"); final String token = "123456"; final SHA1Hash hash = SHA1Hash.saltedHash(token); Assert.assertNotNull(hash); final String hashedToken = hash.toString(); Assert.assertNotNull(hashedToken); Assert.assertFalse(hashedToken.isEmpty()); Assert.assertFalse(token.equals(hashedToken)); final String salt = hash.getSalt(); Assert.assertNotNull(salt); Assert.assertFalse(salt.isEmpty()); }
@Test(expected = IllegalArgumentException.class) public void testSaltedHashWithInvalidSalt() { LOG.info("testSaltedHashWithInvalidSalt"); final String token = "123456"; final String referenceSalt = "78G0"; SHA1Hash.saltedHash(token, referenceSalt); }