1616
1717package io .aiven .kafka .connect .transforms ;
1818
19- import java .security .MessageDigest ;
20- import java .security .NoSuchAlgorithmException ;
21- import java .util .Base64 ;
2219import java .util .HashMap ;
2320import java .util .Map ;
2421
@@ -40,7 +37,32 @@ abstract class HashTest {
4037
4138 private static final String FIELD = "email" ;
4239 private static final String EMPTY_FIELD_VALUE = "" ;
43- private static final String NON_EMPTY_FIELD_VALUE = "jerry@all_your_bases.com" ;
40+ private static final String NON_EMPTY_FIELD_VALUE =
"[email protected] " ;
41+
42+ private static final Map <String , Map <String , String >> HASHED_VALUES = new HashMap <>();
43+
44+ static {
45+ HASHED_VALUES .put ("md5" , new HashMap <>());
46+ // echo -n "" | md5sum -t
47+ HASHED_VALUES .get ("md5" ).put (EMPTY_FIELD_VALUE , "d41d8cd98f00b204e9800998ecf8427e" );
48+ // echo -n "[email protected] " | md5sum -t 49+ HASHED_VALUES .get ("md5" ).put (NON_EMPTY_FIELD_VALUE , "10e5756d5d4c9c1cadd5e1b952071378" );
50+
51+ HASHED_VALUES .put ("sha1" , new HashMap <>());
52+ // echo -n "" | sha1sum -t
53+ HASHED_VALUES .get ("sha1" ).put (EMPTY_FIELD_VALUE , "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
54+ // echo -n "[email protected] " | sha1sum -t 55+ HASHED_VALUES .get ("sha1" ).put (NON_EMPTY_FIELD_VALUE , "dd9ab6e93603bf618db0894a82da64f1623a94b6" );
56+
57+ HASHED_VALUES .put ("sha256" , new HashMap <>());
58+ // echo -n "" | sha256sum -t
59+ HASHED_VALUES .get ("sha256" ).put (EMPTY_FIELD_VALUE ,
60+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" );
61+ // echo -n "[email protected] " | sha256sum -t 62+ HASHED_VALUES .get ("sha256" ).put (NON_EMPTY_FIELD_VALUE ,
63+ "20e85b05e7349963fc64746fbc7f3f4fdf31507921360847ebef333b229cf2d6" );
64+ }
65+
4466 private static final String DEFAULT_HASH_FUNCTION = HashConfig .HashFunction .SHA256 .toString ();
4567 private static final String UNAFFECTED_FIELD = "name" ;
4668 private static final String UNAFFECTED_FIELD_VALUE = "jerry" ;
@@ -232,6 +254,20 @@ void fieldName_EmptyStringValue(final String hashFunction) {
232254 assertEquals (setNewValue (originalRecord , newValue ), result );
233255 }
234256
257+ @ ParameterizedTest
258+ @ ValueSource (strings = {"md5" , "sha1" , "sha256" })
259+ void sameValueSameHash (final String hashFunction ) {
260+ final Schema schema = SchemaBuilder .STRING_SCHEMA ;
261+ final Hash <SinkRecord > transform = transformation (null , false , hashFunction );
262+
263+ for (int i = 0 ; i < 10 ; i ++) {
264+ final SinkRecord originalRecord = record (schema , NON_EMPTY_FIELD_VALUE );
265+ final SinkRecord result = transform .apply (originalRecord );
266+ final String newValue = hash (hashFunction , NON_EMPTY_FIELD_VALUE );
267+ assertEquals (setNewValue (originalRecord , newValue ), result );
268+ }
269+ }
270+
235271 private Hash <SinkRecord > transformation (
236272 final String fieldName ,
237273 final boolean skipMissingOrNull ,
@@ -277,24 +313,6 @@ private SinkRecord setNewValue(final SinkRecord record, final Object newValue) {
277313 }
278314
279315 private String hash (final String function , final String value ) {
280- try {
281- final MessageDigest md ;
282- switch (function ) {
283- case "md5" :
284- md = MessageDigest .getInstance ("MD5" );
285- break ;
286- case "sha1" :
287- md = MessageDigest .getInstance ("SHA1" );
288- break ;
289- case "sha256" :
290- md = MessageDigest .getInstance ("SHA-256" );
291- break ;
292- default :
293- throw new IllegalArgumentException (function );
294- }
295- return Base64 .getEncoder ().encodeToString (md .digest (value .getBytes ()));
296- } catch (final NoSuchAlgorithmException e ) {
297- throw new RuntimeException (e );
298- }
316+ return HASHED_VALUES .get (function ).get (value );
299317 }
300318}
0 commit comments