Skip to content

Commit 2c83925

Browse files
committed
replace getBytes usage
1 parent 06a322a commit 2c83925

File tree

2 files changed

+6
-3
lines changed
  • x-pack/plugin/esql/src
    • main/java/org/elasticsearch/xpack/esql/expression/function/scalar/hash
    • test/java/org/elasticsearch/xpack/esql/expression/function/scalar/hash

2 files changed

+6
-3
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/hash/Hash.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ static BytesRef processConstant(@Fixed(build = true) MessageDigest alg, BytesRef
8484
}
8585

8686
private static BytesRef hash(MessageDigest alg, BytesRef input) {
87-
return new BytesRef(HexFormat.of().formatHex(alg.digest(input.utf8ToString().getBytes())));
87+
alg.update(input.bytes, input.offset, input.length);
88+
var result = alg.digest();
89+
return new BytesRef(HexFormat.of().formatHex(result));
8890
}
8991

9092
@Override

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/hash/HashTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
1818
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
1919

20+
import java.nio.charset.StandardCharsets;
2021
import java.security.MessageDigest;
2122
import java.security.NoSuchAlgorithmException;
2223
import java.util.ArrayList;
@@ -56,7 +57,7 @@ private static TestCaseSupplier createTestCase(String alg, DataType algType, Dat
5657
return new TestCaseSupplier.TestCase(
5758
List.of(
5859
new TestCaseSupplier.TypedData(new BytesRef(alg), algType, "alg"),
59-
new TestCaseSupplier.TypedData(input, inputType, "input")
60+
new TestCaseSupplier.TypedData(new BytesRef(input), inputType, "input")
6061
),
6162
"HashEvaluator[alg=Attribute[channel=0], input=Attribute[channel=1]]",
6263
DataType.KEYWORD,
@@ -67,7 +68,7 @@ private static TestCaseSupplier createTestCase(String alg, DataType algType, Dat
6768

6869
private static String hash(String alg, String input) {
6970
try {
70-
return HexFormat.of().formatHex(MessageDigest.getInstance(alg).digest(input.getBytes()));
71+
return HexFormat.of().formatHex(MessageDigest.getInstance(alg).digest(input.getBytes(StandardCharsets.UTF_8)));
7172
} catch (NoSuchAlgorithmException e) {
7273
throw new IllegalArgumentException("Unknown algorithm: " + alg);
7374
}

0 commit comments

Comments
 (0)