Skip to content

Commit

Permalink
replace getBytes usage
Browse files Browse the repository at this point in the history
  • Loading branch information
idegtiarenko committed Dec 4, 2024
1 parent 06a322a commit 2c83925
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ static BytesRef processConstant(@Fixed(build = true) MessageDigest alg, BytesRef
}

private static BytesRef hash(MessageDigest alg, BytesRef input) {
return new BytesRef(HexFormat.of().formatHex(alg.digest(input.utf8ToString().getBytes())));
alg.update(input.bytes, input.offset, input.length);
var result = alg.digest();
return new BytesRef(HexFormat.of().formatHex(result));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,7 +57,7 @@ private static TestCaseSupplier createTestCase(String alg, DataType algType, Dat
return new TestCaseSupplier.TestCase(
List.of(
new TestCaseSupplier.TypedData(new BytesRef(alg), algType, "alg"),
new TestCaseSupplier.TypedData(input, inputType, "input")
new TestCaseSupplier.TypedData(new BytesRef(input), inputType, "input")
),
"HashEvaluator[alg=Attribute[channel=0], input=Attribute[channel=1]]",
DataType.KEYWORD,
Expand All @@ -67,7 +68,7 @@ private static TestCaseSupplier createTestCase(String alg, DataType algType, Dat

private static String hash(String alg, String input) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance(alg).digest(input.getBytes()));
return HexFormat.of().formatHex(MessageDigest.getInstance(alg).digest(input.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Unknown algorithm: " + alg);
}
Expand Down

0 comments on commit 2c83925

Please sign in to comment.