From 9989505440c46c269d5baf05b4583815db18f002 Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 24 Dec 2024 17:05:25 +1100 Subject: [PATCH] Implement configurable algorithm with sys prop --- .../scavenger/util/HashGenerator.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/scavenger-model/src/main/java/com/navercorp/scavenger/util/HashGenerator.java b/scavenger-model/src/main/java/com/navercorp/scavenger/util/HashGenerator.java index 82d8295..823275b 100644 --- a/scavenger-model/src/main/java/com/navercorp/scavenger/util/HashGenerator.java +++ b/scavenger-model/src/main/java/com/navercorp/scavenger/util/HashGenerator.java @@ -10,9 +10,31 @@ public class HashGenerator { + public enum HashAlgorithm { + + MURMUR(Murmur::from), + SHA256(Sha256::from), + MD5(Md5::from); + + private final Function hashFunction; + + HashAlgorithm(Function hashFunction) { + this.hashFunction = hashFunction; + } + + public String hash(String signature) { + return hashFunction.apply(signature); + } + } + public static class DefaultHash { + private static final String SELECTED_ALGORITHM_PROP = "hash.algorithm"; + private static final HashAlgorithm DEFAULT_ALGORITHM = HashAlgorithm.MD5; + private static final HashAlgorithm SELECTED_ALGORITHM = + HashAlgorithm.valueOf(System.getProperty(SELECTED_ALGORITHM_PROP, DEFAULT_ALGORITHM.name()).toUpperCase()); + public static String from(String signature) { - return Murmur.from(signature); + return SELECTED_ALGORITHM.hash(signature); } }