[go: nahoru, domu]

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

fix: Base64 decoding to discard newline characters #1941

Merged
merged 7 commits into from
May 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
implementation
  • Loading branch information
suztomo committed May 15, 2024
commit 0a4bd6497d7002f7b6fc5ff27c9f26b2bc81f569
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
@Deprecated
public class Base64 {
// Special decoders that discards the new line character so that the behavior matches what
// we had with Apache Commmons Codec's decodeBase64.

Choose a reason for hiding this comment

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

I think it would be worth commenting on what the 64 means. (It doesn't mean very much, since it's only used for encoding and we don't do that.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added explanation of the 2nd parameter of the withSeparator method.

private static final BaseEncoding BASE64 = BaseEncoding.base64().withSeparator("\n", 64);
private static final BaseEncoding BASE64URL = BaseEncoding.base64Url().withSeparator("\n", 64);

/**
* Encodes binary data using the base64 algorithm but does not chunk the output.
Expand Down Expand Up @@ -92,6 +96,9 @@ public static byte[] decodeBase64(byte[] base64Data) {
* Decodes a Base64 String into octets. Note that this method handles both URL-safe and
* non-URL-safe base 64 encoded strings.
*
* <p>For the compatibility with the old version that used Apache Commons Coded's decodeBase64,
* this method discards new line characters and trailing whitespaces.
*
* @param base64String String containing Base64 data or {@code null} for {@code null} result
* @return Array containing decoded data or {@code null} for {@code null} input
*/
Expand All @@ -100,10 +107,10 @@ public static byte[] decodeBase64(String base64String) {
return null;
}
try {
return BaseEncoding.base64().decode(base64String);
return BASE64.decode(base64String);
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof DecodingException) {
return BaseEncoding.base64Url().decode(base64String.trim());
return BASE64URL.decode(base64String.trim());
}
throw e;
}
Expand Down
Loading