Skip to content

Commit 0d24c31

Browse files
committed
Refactor: update sha2 implementations with DigestUtils
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent d2711f2 commit 0d24c31

1 file changed

Lines changed: 13 additions & 42 deletions

File tree

core/src/main/java/org/opensearch/sql/expression/function/udf/CryptographicFunction.java

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
package org.opensearch.sql.expression.function.udf;
77

8-
import java.security.MessageDigest;
9-
import java.security.NoSuchAlgorithmException;
108
import java.util.List;
11-
import java.util.Map;
129
import org.apache.calcite.adapter.enumerable.NotNullImplementor;
1310
import org.apache.calcite.adapter.enumerable.NullPolicy;
1411
import org.apache.calcite.adapter.enumerable.RexToLixTranslator;
@@ -18,7 +15,9 @@
1815
import org.apache.calcite.sql.type.ReturnTypes;
1916
import org.apache.calcite.sql.type.SqlReturnTypeInference;
2017
import org.apache.calcite.sql.type.SqlTypeTransforms;
18+
import org.apache.commons.codec.binary.Hex;
2119
import org.apache.commons.codec.digest.DigestUtils;
20+
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
2221
import org.opensearch.sql.expression.function.ImplementorUDF;
2322

2423
public class CryptographicFunction extends ImplementorUDF {
@@ -36,52 +35,24 @@ public SqlReturnTypeInference getReturnTypeInference() {
3635
}
3736

3837
public static class Sha2Implementor implements NotNullImplementor {
39-
private static final ThreadLocal<Map<Integer, MessageDigest>> digests;
40-
41-
static {
42-
digests =
43-
ThreadLocal.withInitial(
44-
() -> {
45-
try {
46-
return Map.of(
47-
224,
48-
MessageDigest.getInstance("SHA-224"),
49-
256,
50-
DigestUtils.getSha256Digest(),
51-
384,
52-
DigestUtils.getSha384Digest(),
53-
512,
54-
DigestUtils.getSha512Digest());
55-
} catch (NoSuchAlgorithmException e) {
56-
throw new RuntimeException(e);
57-
}
58-
});
59-
}
60-
6138
@Override
6239
public Expression implement(
6340
RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) {
6441
return Expressions.call(Sha2Implementor.class, "getDigest", translatedOperands);
6542
}
6643

6744
public static String getDigest(String input, int algorithm) {
68-
if (!digests.get().containsKey(algorithm)) {
69-
throw new IllegalArgumentException("Unsupported SHA2 algorithm: " + algorithm);
70-
}
71-
return getDigest(digests.get().get(algorithm), input);
72-
}
73-
74-
private static String getDigest(MessageDigest digest, String input) {
75-
byte[] hash = digest.digest(input.getBytes());
76-
StringBuilder hexString = new StringBuilder();
77-
for (byte b : hash) {
78-
String hex = Integer.toHexString(0xff & b);
79-
if (hex.length() == 1) {
80-
hexString.append('0');
81-
}
82-
hexString.append(hex);
83-
}
84-
return hexString.toString();
45+
return switch (algorithm) {
46+
case 224 -> Hex.encodeHexString(
47+
DigestUtils.getDigest(MessageDigestAlgorithms.SHA_224).digest(input.getBytes()));
48+
case 256 -> DigestUtils.sha256Hex(input);
49+
case 384 -> DigestUtils.sha384Hex(input);
50+
case 512 -> DigestUtils.sha512Hex(input);
51+
default -> throw new IllegalArgumentException(
52+
String.format(
53+
"Unsupported SHA2 algorithm: %d. Only 224, 256, 384, and 512 are supported.",
54+
algorithm));
55+
};
8556
}
8657
}
8758
}

0 commit comments

Comments
 (0)