1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_BASE64URL_H_
6 #define BASE_BASE64URL_H_
11 #include "base/base_export.h"
12 #include "base/containers/span.h"
13 #include "base/strings/string_piece.h"
14 #include "third_party/abseil-cpp/absl/types/optional.h"
18 enum class Base64UrlEncodePolicy {
19 // Include the trailing padding in the output, when necessary.
22 // Remove the trailing padding from the output.
26 // Encodes the |input| binary data in base64url, defined in RFC 4648:
27 // https://tools.ietf.org/html/rfc4648#section-5
29 // The |policy| defines whether padding should be included or omitted from the
30 // encoded |*output|. |input| and |*output| may reference the same storage.
31 BASE_EXPORT void Base64UrlEncode(span<const uint8_t> input,
32 Base64UrlEncodePolicy policy,
35 // Same as the previous function, but accepts an input string.
36 BASE_EXPORT void Base64UrlEncode(StringPiece input,
37 Base64UrlEncodePolicy policy,
40 enum class Base64UrlDecodePolicy {
41 // Require inputs contain trailing padding if non-aligned.
44 // Accept inputs regardless of whether or not they have the correct padding.
47 // Reject inputs if they contain any trailing padding.
51 // Decodes the |input| string in base64url, defined in RFC 4648:
52 // https://tools.ietf.org/html/rfc4648#section-5
54 // The |policy| defines whether padding will be required, ignored or disallowed
55 // altogether. |input| and |*output| may reference the same storage.
56 [[nodiscard]] BASE_EXPORT bool Base64UrlDecode(StringPiece input,
57 Base64UrlDecodePolicy policy,
60 // Same as the previous function, but writing to a `std::vector`.
61 [[nodiscard]] BASE_EXPORT absl::optional<std::vector<uint8_t>> Base64UrlDecode(
63 Base64UrlDecodePolicy policy);
67 #endif // BASE_BASE64URL_H_