Skip to content

Instantly share code, notes, and snippets.

@AdilHoumadi
Created July 14, 2021 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdilHoumadi/b0adec2e9b1cdbce5b8cc920ba71c7dd to your computer and use it in GitHub Desktop.
Save AdilHoumadi/b0adec2e9b1cdbce5b8cc920ba71c7dd to your computer and use it in GitHub Desktop.
Example of hashing function
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