DefaultHasher implements Hasher { @Override public String getHash(final Object toHash) throws EncoderException, NoSuchAlgorithmException { final MessageDigest messageDigest = MessageDigest.getInstance(algorithm); if (toHash == null) { return StringUtils.repeat("00", messageDigest.getDigestLength()); } else if (toHash instanceof String) { return getHash(messageDigest, toHash.toString().getBytes(charset)); } else if (toHash instanceof Serializable) { final byte[] serialized = SerializationUtils.serialize((Serializable) toHash); return getHash(messageDigest, serialized); } return null; } DefaultHasher(final String algorithm, final BinaryEncoder encoder, final Charset charset);  DefaultHasher(final String algorithm, final BinaryEncoder encoder);  DefaultHasher(final String algorithm); @Override String getHash(final Object toHash); @Override void configure(Optional<Map<String, Object>> config); static final Set<String> supportedHashes();  }
@Test public void getHashAsHexOfNullValueReturnsPadded00() throws Exception { assertEquals(StringUtils.repeat("00", 16), new DefaultHasher("md5", encoder).getHash(null)); assertEquals(StringUtils.repeat("00", 32), new DefaultHasher("sha-256", encoder).getHash(null)); }
@Test public void nonSerializableShouldReturnNull() throws Exception { assertNull(new DefaultHasher("md5", encoder, charset).getHash(new Object())); }
@Test public void hashingStringWithEmptyString() throws Exception { assertEquals("d41d8cd98f00b204e9800998ecf8427e", new DefaultHasher("md5", encoder).getHash("")); }
@Test public void hashingSerializableObject() throws Exception { final Collection<String> serializable = Collections.emptyList(); assertEquals("ef5e8c8d27af3a953b4674065c99a52a", new DefaultHasher("md5", encoder, charset).getHash(serializable)); }