Skip to content

Use HexFormat instead of custom Hex codec - #19641

Open
therepanic wants to merge 1 commit into
spring-projects:mainfrom
therepanic:hex
Open

Use HexFormat instead of custom Hex codec#19641
therepanic wants to merge 1 commit into
spring-projects:mainfrom
therepanic:hex

Conversation

@therepanic

Copy link
Copy Markdown
Contributor

Replace internal org.springframework.security.crypto.codec.Hex utility class with standard Java 17 java.util.HexFormat. We already did something similar in this module earlier: 2f53a2e

Replace internal org.springframework.security.crypto.codec.Hex utility class with standard Java 17 java.util.HexFormat. We already did something similar in this module earlier: 2f53a2e

Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
Comment on lines -85 to +84
return new String(Hex.encode(sha(data)));
return HexFormat.of().formatHex(sha(data));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the string was created without regard for the string pool, but now the string pool is being used. Perhaps it should be wrapped in new String()?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking. I believe the code is fine as-is. The new String() from before and Hex.encode returning char[] was not about preventing the value from entering the String pool. In that case, to add it, someone would need to call .intern().

And since HexFormat does not do that, the interning semantics of this method do not change with your edit. 👍

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 1, 2026
@therepanic

Copy link
Copy Markdown
Contributor Author

It also renders #19023 unnecessary.

@jzheaux jzheaux left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @therepanic! It's great to be able to change to use official libraries.

In addition to my inline feedback, will you please also still deprecate Hex? We need to do this since it is a public class. Will you add a @Deprecated annotation as well as a @deprecated javadoc note that indicates folks should use HexFormat?

This can go in an earlier commit so you can reference the other ticket you created.

Comment on lines -85 to +84
return new String(Hex.encode(sha(data)));
return HexFormat.of().formatHex(sha(data));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking. I believe the code is fine as-is. The new String() from before and Hex.encode returning char[] was not about preventing the value from entering the String pool. In that case, to add it, someone would need to call .intern().

And since HexFormat does not do that, the interning semantics of this method do not change with your edit. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants