@Test public void testGetSecretKey() throws Exception { SecretKeyProvider keyProvider = Sha1HashSecretKeyProvider.newAesInstance(); final byte[] expectedBytes = {98, -112, 13, -94, -67, 30, -7, 80, -94, -111, 39, -22, 85, -90, -29, -111}; byte[] salt = new byte[]{-42, -49, -27, -25, 108, -125, 71, -68, -128, 49, 104, -2, -122, 31, 105, -4}; final SecretKey actual = keyProvider.getSecretKey("1234", salt); final SecretKey expected = keyProvider.getSecretKey("1234", salt); Assert.assertTrue(Objects.areEqual(actual.getEncoded(), expected.getEncoded(), ByteArrayEqualizer.getInstance())); Assert.assertTrue(Objects.areEqual(expectedBytes, actual.getEncoded(), ByteArrayEqualizer.getInstance())); salt[0] = (byte) (salt[0] - 1); final SecretKey another = keyProvider.getSecretKey("1234", salt); final byte[] anotherExpectedBytes = {123, -15, 62, 11, -96, 104, -11, -67, -17, -104, 47, -47, -53, 89, 85, 15}; Assert.assertFalse(Objects.areEqual(actual.getEncoded(), another.getEncoded(), ByteArrayEqualizer.getInstance())); Assert.assertTrue(Objects.areEqual(anotherExpectedBytes, another.getEncoded(), ByteArrayEqualizer.getInstance())); }
@Nonnull @Override public SecretKey getSecretKey(@Nonnull String secret, @Nonnull byte[] salt) throws CiphererException { final String secretKey = secret + Bytes.toHex(salt); try { final MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] secretKeyHash = sha.digest(secretKey.getBytes(Charsets.UTF_8)); secretKeyHash = Arrays.copyOf(secretKeyHash, saltLength); return new SecretKeySpec(secretKeyHash, ciphererAlgorithm); } catch (NoSuchAlgorithmException e) { throw new CiphererException(e); } }
Sha1HashSecretKeyProvider implements SecretKeyProvider { @Nonnull @Override public SecretKey getSecretKey(@Nonnull String secret, @Nonnull byte[] salt) throws CiphererException { final String secretKey = secret + Bytes.toHex(salt); try { final MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] secretKeyHash = sha.digest(secretKey.getBytes(Charsets.UTF_8)); secretKeyHash = Arrays.copyOf(secretKeyHash, saltLength); return new SecretKeySpec(secretKeyHash, ciphererAlgorithm); } catch (NoSuchAlgorithmException e) { throw new CiphererException(e); } } }
Sha1HashSecretKeyProvider implements SecretKeyProvider { @Nonnull @Override public SecretKey getSecretKey(@Nonnull String secret, @Nonnull byte[] salt) throws CiphererException { final String secretKey = secret + Bytes.toHex(salt); try { final MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] secretKeyHash = sha.digest(secretKey.getBytes(Charsets.UTF_8)); secretKeyHash = Arrays.copyOf(secretKeyHash, saltLength); return new SecretKeySpec(secretKeyHash, ciphererAlgorithm); } catch (NoSuchAlgorithmException e) { throw new CiphererException(e); } } private  Sha1HashSecretKeyProvider(@Nonnull String ciphererAlgorithm, int saltLength); }
Sha1HashSecretKeyProvider implements SecretKeyProvider { @Nonnull @Override public SecretKey getSecretKey(@Nonnull String secret, @Nonnull byte[] salt) throws CiphererException { final String secretKey = secret + Bytes.toHex(salt); try { final MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] secretKeyHash = sha.digest(secretKey.getBytes(Charsets.UTF_8)); secretKeyHash = Arrays.copyOf(secretKeyHash, saltLength); return new SecretKeySpec(secretKeyHash, ciphererAlgorithm); } catch (NoSuchAlgorithmException e) { throw new CiphererException(e); } } private  Sha1HashSecretKeyProvider(@Nonnull String ciphererAlgorithm, int saltLength); @Nonnull static SecretKeyProvider newAesInstance(); @Nonnull static SecretKeyProvider newDesInstance(); @Nonnull static Sha1HashSecretKeyProvider newInstance(@Nonnull String ciphererAlgorithm, int saltLength); @Nonnull @Override SecretKey getSecretKey(@Nonnull String secret, @Nonnull byte[] salt); }
Sha1HashSecretKeyProvider implements SecretKeyProvider { @Nonnull @Override public SecretKey getSecretKey(@Nonnull String secret, @Nonnull byte[] salt) throws CiphererException { final String secretKey = secret + Bytes.toHex(salt); try { final MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] secretKeyHash = sha.digest(secretKey.getBytes(Charsets.UTF_8)); secretKeyHash = Arrays.copyOf(secretKeyHash, saltLength); return new SecretKeySpec(secretKeyHash, ciphererAlgorithm); } catch (NoSuchAlgorithmException e) { throw new CiphererException(e); } } private  Sha1HashSecretKeyProvider(@Nonnull String ciphererAlgorithm, int saltLength); @Nonnull static SecretKeyProvider newAesInstance(); @Nonnull static SecretKeyProvider newDesInstance(); @Nonnull static Sha1HashSecretKeyProvider newInstance(@Nonnull String ciphererAlgorithm, int saltLength); @Nonnull @Override SecretKey getSecretKey(@Nonnull String secret, @Nonnull byte[] salt);  }