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

Trim = from Base64UrlSafeEncode #691

Merged
merged 4 commits into from
Aug 27, 2024
Merged

Trim = from Base64UrlSafeEncode #691

merged 4 commits into from
Aug 27, 2024

Conversation

hishamco
Copy link
Collaborator

Fixes #690

Copy link
Owner

@sebastienros sebastienros left a comment

Choose a reason for hiding this comment

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

Can you add to the unit tests?

Hell -> SGVsbA== -> SGVsbA
Hello -> SGVsbG8= -> SGVsbG8

@@ -196,7 +196,7 @@ public static ValueTask<FluidValue> Base64UrlSafeEncode(FluidValue input, Filter
encodedBase64StringBuilder.Replace('+', '-');
encodedBase64StringBuilder.Replace('/', '_');

return new StringValue(encodedBase64StringBuilder.ToString());
return new StringValue(encodedBase64StringBuilder.ToString().TrimEnd('='));
Copy link
Owner

Choose a reason for hiding this comment

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

Does the string builder have something like it? That would prevent the extra string allocation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we know that base64 encoded version ends with == (it should) it should suffice to encodedBase64StringBuilder.Length -= 2;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good option too

Copy link
Owner

Choose a reason for hiding this comment

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

It's not always the case, could be zero, one or two chars. See the test cases I shared for instance.

Copy link
Owner

Choose a reason for hiding this comment

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

But I see what you mean now, sorry.

Copy link
Collaborator Author

@hishamco hishamco Aug 22, 2024

Choose a reason for hiding this comment

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

The issue that might be raise during decoding how we know there was zero, one or two "=" on the encoded URL?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FYI there's Base64.IsValid() introduced in .NET 8.0 but it will not solve the problem in our case

@sebastienros
Copy link
Owner

Feel free to merge when you add the test, thanks.

@hishamco
Copy link
Collaborator Author

Sure, I will add a test then merge

@sebastienros
Copy link
Owner

Added the same tests to ensure the decoding was fine too and it happens it was not as the = chars are required by the API.

Took the time to implement @lahma 's suggestion to skip the string allocation.

@sebastienros sebastienros merged commit de1fa85 into main Aug 27, 2024
1 check passed
@sebastienros sebastienros deleted the hishamco/base64 branch August 27, 2024 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Base64UrlSafeEncode should remove ==
3 participants