Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHA1/256 functions is only possible to use for String, it's impossible for byte[] in painless #108718

Open
ser-sergeev opened this issue May 16, 2024 · 6 comments
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement Team:Core/Infra Meta label for core/infra team

Comments

@ser-sergeev
Copy link

Description

No response

@ser-sergeev ser-sergeev added >enhancement needs:triage Requires assignment of a team area label labels May 16, 2024
@slobodanadamovic
Copy link
Contributor

Thank you very much for your interest in Elasticsearch.

This appears to be a user question, and we'd like to direct these to the Elasticsearch forum. If you can stop by there, we'd appreciate it. This allows us to use GitHub for verified bug reports, feature requests, and pull requests.

There's an active community in the forum that should be able to help get an answer to your question. As such, I hope you don't mind that I close this.

@ser-sergeev
Copy link
Author

@slobodanadamovic The feature requests also must be done on forum? Right now there is no way to calculate a hash for bytes, only for String.

@slobodanadamovic
Copy link
Contributor

slobodanadamovic commented May 16, 2024

@ser-sergeev Apologies, I've interpreted this as a question. This is a correct place to raise an enhancement requests.
But in your case, I believe you could solve this by converting the byte[] to String and then call sha1() or sha256(). Here is what I have in mind:

byte[] bytes = doc['myByteField'].value;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
  stringBuilder.append((char)(bytes[i] & 0xFF));
}
return stringBuilder.toString().sha256();

@ser-sergeev
Copy link
Author

@slobodanadamovic Pardon me, I thought, because of UTF-8 encoding, I wouldn't be able to convert from bytes to String.
Sorry for interrupting from a real work. I'm really sorry.

Anyway, big thank you for your answer and patience.

@ser-sergeev
Copy link
Author

ser-sergeev commented May 17, 2024

@slobodanadamovic No, sha256(bytes) and sha256('bytes->String') isn't equal. I compared the result of the code above and the result of sha265 with the same bytes.

@slobodanadamovic slobodanadamovic added :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache Team:Core/Infra Meta label for core/infra team and removed needs:triage Requires assignment of a team area label labels May 24, 2024
@slobodanadamovic
Copy link
Contributor

@ser-sergeev I see now that there can be potential issues with wrong encoding. Let me tag the responsible team for painless to get their input on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement Team:Core/Infra Meta label for core/infra team
Projects
None yet
Development

No branches or pull requests

2 participants