-
-
Save AdilHoumadi/b0adec2e9b1cdbce5b8cc920ba71c7dd to your computer and use it in GitHub Desktop.
Example of hashing function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adidas.fdp; | |
import com.google.common.collect.ImmutableMap; | |
import org.apache.commons.codec.digest.DigestUtils; | |
import static org.apache.commons.codec.digest.MessageDigestAlgorithms.*; | |
public class Utils { | |
private static final ImmutableMap<String, Hasher> HASHER_MAP = | |
new ImmutableMap.Builder<String, Hasher>() | |
.put(SHA_256, new Utils.Sha256()) | |
.put(SHA3_256, new Utils.Sha3_256()) | |
.put(SHA_512, new Utils.Sha512()) | |
.put(SHA3_512, new Utils.Sha3_512()) | |
.build(); | |
public static class Sha256 implements Hasher { | |
@Override | |
public String apply(final String input) { | |
return DigestUtils.sha256Hex(input); | |
} | |
} | |
public static class Sha3_256 implements Hasher { | |
@Override | |
public String apply(final String input) { | |
return DigestUtils.sha3_256Hex(input); | |
} | |
} | |
public static class Sha512 implements Hasher { | |
@Override | |
public String apply(final String input) { | |
return DigestUtils.sha512Hex(input); | |
} | |
} | |
public static class Sha3_512 implements Hasher { | |
@Override | |
public String apply(final String input) { | |
return DigestUtils.sha3_512Hex(input); | |
} | |
} | |
public static void checkAlgorithm(String algo) { | |
String SupportedAlgorithm = String.join(", ", HASHER_MAP.keySet()); | |
if (HASHER_MAP.get(algo) == null) { | |
throw new KsqlFunctionException("Unsupported Algorithm " | |
+ algo + "- The supported algorithm are the following: " + SupportedAlgorithm); | |
} | |
} | |
public static String hashString(String str, String algo) { | |
return HASHER_MAP.get(algo).apply(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment