Skip to content

Commit 224136d

Browse files
committed
Pitch: Base64 urlencoding and omitting padding options
1 parent b673ae7 commit 224136d

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Adding base64 urlencoding and omitting padding option to base64 encoding and decoding
2+
3+
* Proposal: SF-NNNN
4+
* Authors: [Fabian Fett](https://github.com/fabianfett)
5+
* Review Manager: TBD
6+
* Status: **Awaiting implementation**
7+
* Bug: *if applicable* [apple/swift#NNNNN](https://github.com/apple/swift-foundation/issues/NNNNN)
8+
* Implementation: [apple/swift-foundation#NNNNN](https://github.com/apple/swift-foundation/pull/NNNNN)
9+
* Review: ([pitch](https://forums.swift.org/...))
10+
11+
## Revision history
12+
13+
* **v1** Initial version
14+
15+
## Introduction
16+
17+
Introducing base64 encoding and decoding options to support the base64url alphabet as defined in [RFC4648] and to allow the omission of padding characters.
18+
19+
## Motivation
20+
21+
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:
22+
23+
- [RFC7519 - JSON Web Token (JWT)][RFC7519]
24+
- [RFC8291 - Message Encryption for Web Push][RFC8291]
25+
26+
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.
27+
28+
## Solution
29+
30+
We propose to add additional options to `Data.Base64EncodingOptions`:
31+
32+
```swift
33+
extension Data {
34+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
35+
public struct Base64EncodingOptions : OptionSet, Sendable {
36+
/// Use the base64url alphabet to encode the data
37+
@available(FoundationPreview 6.1, *)
38+
public static var base64UrlAlphabet: Base64EncodingOptions { get }
39+
40+
/// Omit the `=` padding characters in the end of the base64 encoded result
41+
@available(FoundationPreview 6.1, *)
42+
public static var omitPaddingCharacter: Base64EncodingOptions { get }
43+
}
44+
}
45+
```
46+
47+
Simultaneously we will add the same options to `Data.Base64EncodingOptions` 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.
48+
49+
```swift
50+
extension Data {
51+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
52+
public struct Base64DecodingOptions : OptionSet, Sendable {
53+
/// Modify the decoding algorithm so that it ignores unknown non-Base-64 bytes, including line ending characters.
54+
///
55+
/// - Warning: Using `ignoreUnknownCharacters` might allow the decoding of base64url data, even when the
56+
/// `base64UrlAlphabet` is not selected. It might also allow using the base64 alphabet when the
57+
/// `base64UrlAlphabet` is selected.
58+
/// Consider using the `ignoreWhitespaceCharacters` option if possible.
59+
public static let ignoreUnknownCharacters = Base64DecodingOptions(rawValue: 1 << 0)
60+
61+
/// Modify the decoding algorithm so that it ignores whitespace characters (CR LF Tab and Space).
62+
///
63+
/// The decoding will fail if any other invalid character is found in the encoded data.
64+
@available(FoundationPreview 6.1, *)
65+
public static var ignoreWhitespaceCharacters: Base64EncodingOptions { get }
66+
67+
/// Modify the decoding algorithm so that it expects base64 encoded data that uses base64url alphabet.
68+
@available(FoundationPreview 6.1, *)
69+
public static var base64UrlAlphabet: Base64EncodingOptions { get }
70+
71+
/// Modify the decoding algorithm so that it expects no padding characters at the end of the encoded data.
72+
///
73+
/// The decoding will fail if the padding character `=` is used at the end of the encoded data.
74+
///
75+
/// - Warning: This option is ignored if `ignoreUnknownCharacters` is used at the same time. Consider
76+
/// using `ignoreWhitespaceCharacters` if possible.
77+
@available(FoundationPreview 6.1, *)
78+
public static var omitPaddingCharacter: Base64EncodingOptions { get }
79+
}
80+
}
81+
```
82+
83+
## Impact on existing code
84+
85+
None. This is an additive change.
86+
87+
[RFC4648]: https://datatracker.ietf.org/doc/html/rfc4648
88+
[RFC7519]: https://datatracker.ietf.org/doc/html/rfc7519
89+
[RFC8291]: https://datatracker.ietf.org/doc/html/rfc8291

0 commit comments

Comments
 (0)