-
Notifications
You must be signed in to change notification settings - Fork 189
[Proposal] Base64 urlencoding and omitting padding options #1156
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
Open
fabianfett
wants to merge
3
commits into
swiftlang:main
Choose a base branch
from
fabianfett:ff-pitch-add-base64-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,82 @@ | ||
# Adding base64 urlencoding and omitting padding option to base64 encoding and decoding | ||
|
||
* Proposal: [SF-0020](0020-base64-urlencoding-and-omitting-padding.md) | ||
* Authors: [Fabian Fett](https://github.com/fabianfett) | ||
* Review Manager: TBD | ||
* Status: **Awaiting review** | ||
* Implementation: [apple/swift-foundation#1195](https://github.com/swiftlang/swift-foundation/pull/1195) [apple/swift-foundation#1196](https://github.com/swiftlang/swift-foundation/pull/1196) | ||
* Review: [pitch](https://forums.swift.org/t/pitch-adding-base64-urlencoding-and-omitting-padding-options-to-base64-encoding-and-decoding/77659) | ||
|
||
## Revision history | ||
|
||
* **v1** Initial version | ||
|
||
## Introduction | ||
|
||
Introducing base64 encoding and decoding options to support the base64url alphabet as defined in [RFC4648] and to allow the omission of padding characters. | ||
|
||
## Motivation | ||
|
||
Foundation offers APIs to encode data in the base64 format and to decode base64 encoded data. Multiple RFCs that define cryptography for the web use the base64url encoding and strip the padding characters in the end. Examples for this are: | ||
|
||
- [RFC7519 - JSON Web Token (JWT)][RFC7519] | ||
- [RFC8291 - Message Encryption for Web Push][RFC8291] | ||
|
||
Since Foundation is not offering an API to support the base64url alphabet and omitting the padding characters, users create wrappers around the existing APIs using `replacingOccurrences(of:, with:)`. While this approach works it is very inefficient. The data has to be iterated three times, where one time could have been sufficient. | ||
|
||
## Solution | ||
|
||
We propose to add additional options to `Data.Base64EncodingOptions`: | ||
|
||
```swift | ||
extension Data.Base64EncodingOptions { | ||
/// Use the base64url alphabet to encode the data | ||
@available(FoundationPreview 6.2, *) | ||
public static var base64URLAlphabet: Base64EncodingOptions { get } | ||
|
||
/// Omit the `=` padding characters in the end of the base64 encoded result | ||
@available(FoundationPreview 6.2, *) | ||
public static var omitPaddingCharacter: Base64EncodingOptions { get } | ||
} | ||
``` | ||
|
||
Simultaneously we will add the same options to `Data.Base64DecodingOptions` and an additional `ignoreWhitespaceCharacters` option. Please note that we show the existing `ignoreUnknownCharacters` option in the code snippet below, as we intend to change its documentation to better explains its tradeoffs. | ||
|
||
```swift | ||
extension Data.Base64DecodingOptions { | ||
/// Modify the decoding algorithm so that it ignores unknown non-Base-64 bytes, including line ending characters. | ||
/// | ||
/// - Warning: Using `ignoreUnknownCharacters` might allow the decoding of base64url data, even when the | ||
/// `base64URLAlphabet` is not selected. It might also allow using the base64 alphabet when the | ||
/// `base64URLAlphabet` is selected. | ||
/// Consider using the `ignoreWhitespaceCharacters` option if possible. | ||
public static let ignoreUnknownCharacters = Base64DecodingOptions(rawValue: 1 << 0) | ||
|
||
/// Modify the decoding algorithm so that it ignores whitespace characters (CR LF Tab and Space). | ||
/// | ||
/// The decoding will fail if any other invalid character is found in the encoded data. | ||
@available(FoundationPreview 6.2, *) | ||
public static var ignoreWhitespaceCharacters: Base64EncodingOptions { get } | ||
|
||
/// Modify the decoding algorithm so that it expects base64 encoded data that uses base64url alphabet. | ||
@available(FoundationPreview 6.2, *) | ||
public static var base64URLAlphabet: Base64EncodingOptions { get } | ||
|
||
/// Modify the decoding algorithm so that it expects no padding characters at the end of the encoded data. | ||
/// | ||
/// The decoding will fail if the padding character `=` is used at the end of the encoded data. | ||
/// | ||
/// - Warning: This option is ignored if `ignoreUnknownCharacters` is used at the same time. Consider | ||
/// using `ignoreWhitespaceCharacters` if possible. | ||
@available(FoundationPreview 6.2, *) | ||
public static var omitPaddingCharacter: Base64EncodingOptions { get } | ||
} | ||
``` | ||
|
||
## Impact on existing code | ||
|
||
None. This is an additive change. | ||
|
||
[RFC4648]: https://datatracker.ietf.org/doc/html/rfc4648 | ||
[RFC7519]: https://datatracker.ietf.org/doc/html/rfc7519 | ||
[RFC8291]: https://datatracker.ietf.org/doc/html/rfc8291 |
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.
please fix the name scope here (encoding options vs decoding options and the extension they belong)