Commit 4b06a24e authored by Joe Lee's avatar Joe Lee

Add support for url-safe base64 encoding

parent 463052ee
......@@ -475,5 +475,50 @@ KJ_TEST("base64 encoding/decoding") {
}
}
KJ_TEST("base64 url encoding") {
{
// Handles empty.
auto encoded = encodeBase64Url(StringPtr("").asBytes());
KJ_EXPECT(encoded == "", encoded, encoded.size());
}
{
// Handles paddingless encoding.
auto encoded = encodeBase64Url(StringPtr("foo").asBytes());
KJ_EXPECT(encoded == "Zm9v", encoded, encoded.size());
}
{
// Handles padded encoding.
auto encoded1 = encodeBase64Url(StringPtr("quux").asBytes());
KJ_EXPECT(encoded1 == "cXV1eA", encoded1, encoded1.size());
auto encoded2 = encodeBase64Url(StringPtr("corge").asBytes());
KJ_EXPECT(encoded2 == "Y29yZ2U", encoded2, encoded2.size());
}
{
// No line breaks.
StringPtr fullLine = "012345678901234567890123456789012345678901234567890123";
auto encoded = encodeBase64Url(StringPtr(fullLine).asBytes());
KJ_EXPECT(
encoded == "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIz",
encoded);
}
{
// Replaces plusses.
ArrayPtr<const byte> data = { 0b11111011, 0b11101111, 0b10111110 };
auto encoded = encodeBase64Url(data);
KJ_EXPECT(encoded == "----", encoded, encoded.size());
}
{
// Replaces slashes.
ArrayPtr<const byte> data = { 0b11111111, 0b11111111, 0b11111111 };
auto encoded = encodeBase64Url(data);
KJ_EXPECT(encoded == "____", encoded, encoded.size());
}
}
} // namespace
} // namespace kj
......@@ -1001,4 +1001,24 @@ EncodingResult<Array<byte>> decodeBase64(ArrayPtr<const char> input) {
return EncodingResult<Array<byte>>(kj::mv(output), state.hadErrors);
}
String encodeBase64Url(ArrayPtr<const byte> bytes) {
// TODO(perf): Rewrite as single pass?
// TODO(someday): Write decoder?
auto base64 = kj::encodeBase64(bytes);
for (char& c: base64) {
if (c == '+') c = '-';
if (c == '/') c = '_';
}
// Remove trailing '='s.
kj::ArrayPtr<const char> slice = base64;
while (slice.size() > 0 && slice.back() == '=') {
slice = slice.slice(0, slice.size() - 1);
}
return kj::str(slice);
}
} // namespace kj
......@@ -213,6 +213,9 @@ EncodingResult<Array<byte>> decodeBase64(ArrayPtr<const char> text);
// Decode base64 text. This function reports errors required by the WHATWG HTML/Infra specs: see
// https://html.spec.whatwg.org/multipage/webappapis.html#atob for details.
String encodeBase64Url(ArrayPtr<const byte> bytes);
// Encode the given bytes as URL-safe base64 text. (RFC 4648, section 5)
// =======================================================================================
// inline implementation details
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment