55
66package org .opensearch .sql .expression .function .udf ;
77
8- import java .security .MessageDigest ;
9- import java .security .NoSuchAlgorithmException ;
108import java .util .List ;
11- import java .util .Map ;
129import org .apache .calcite .adapter .enumerable .NotNullImplementor ;
1310import org .apache .calcite .adapter .enumerable .NullPolicy ;
1411import org .apache .calcite .adapter .enumerable .RexToLixTranslator ;
1815import org .apache .calcite .sql .type .ReturnTypes ;
1916import org .apache .calcite .sql .type .SqlReturnTypeInference ;
2017import org .apache .calcite .sql .type .SqlTypeTransforms ;
18+ import org .apache .commons .codec .binary .Hex ;
2119import org .apache .commons .codec .digest .DigestUtils ;
20+ import org .apache .commons .codec .digest .MessageDigestAlgorithms ;
2221import org .opensearch .sql .expression .function .ImplementorUDF ;
2322
2423public 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