-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Add Base64Url encoding #4822
Merged
Merged
Add Base64Url encoding #4822
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ffcc360
add encode url to the Base64 library
Amxx c3ef73a
add changeset
Amxx b217af3
refactor base64 encoding
Amxx ea29b55
typo
Amxx 551ad07
cleanup
Amxx fb5dade
Apply suggestions from code review
Amxx 6e3d973
fix tests
Amxx 1f1ad9e
fix lint
Amxx edcdf5b
stable lint
Amxx b9e47dc
Add fuzzing tests
ernestognw ea6d5bb
Enable ffi
ernestognw 66305d9
Change test name lol
ernestognw 6a86d82
Avoid running Base64 fuzzing in CI
ernestognw 7cfe507
Improve Base64 unit tests
ernestognw 4b30a0b
Merge branch 'master' into feature/base64url
ernestognw 26658ce
Merge branch 'master' into feature/base64url
ernestognw c05a170
Merge branch 'master' into feature/base64url
ernestognw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Base64`: Add `encodeURL` following section 5 of RFC4648 for URL encoding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
_encode() { | ||
# - Print the input to stdout | ||
# - Remove the first two characters | ||
# - Convert from hex to binary | ||
# - Convert from binary to base64 | ||
# - Remove newlines from `base64` output | ||
echo -n "$1" | cut -c 3- | xxd -r -p | base64 | tr -d \\n | ||
} | ||
|
||
encode() { | ||
# - Convert from base64 to hex | ||
# - Remove newlines from `xxd` output | ||
_encode "$1" | xxd -p | tr -d \\n | ||
} | ||
|
||
encodeURL() { | ||
# - Remove padding from `base64` output | ||
# - Replace `+` with `-` | ||
# - Replace `/` with `_` | ||
# - Convert from base64 to hex | ||
# - Remove newlines from `xxd` output | ||
_encode "$1" | sed 's/=//g' | sed 's/+/-/g' | sed 's/\//_/g' | xxd -p | tr -d \\n | ||
} | ||
|
||
# $1: function name | ||
# $2: input | ||
$1 $2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
|
||
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol"; | ||
|
||
contract Base64Test is Test { | ||
function testEncode(bytes memory input) external { | ||
string memory output = Base64.encode(input); | ||
assertEq(output, _base64Ffi(input, "encode")); | ||
} | ||
|
||
function testEncodeURL(bytes memory input) external { | ||
string memory output = Base64.encodeURL(input); | ||
assertEq(output, _base64Ffi(input, "encodeURL")); | ||
} | ||
|
||
function _base64Ffi(bytes memory input, string memory fn) internal returns (string memory) { | ||
string[] memory command = new string[](4); | ||
command[0] = "bash"; | ||
command[1] = "scripts/tests/base64.sh"; | ||
command[2] = fn; | ||
command[3] = vm.toString(input); | ||
bytes memory retData = vm.ffi(command); | ||
return string(retData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: We agreed to not include that in the package. The foundry tests will be disabled in CI (until base64 eventually becomes foundray-native)