base64url.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_BASE64URL_H_
  5. #define BASE_BASE64URL_H_
  6. #include <string>
  7. #include "base/base_export.h"
  8. #include "base/strings/string_piece.h"
  9. namespace base {
  10. enum class Base64UrlEncodePolicy {
  11. // Include the trailing padding in the output, when necessary.
  12. INCLUDE_PADDING,
  13. // Remove the trailing padding from the output.
  14. OMIT_PADDING
  15. };
  16. // Encodes the |input| string in base64url, defined in RFC 4648:
  17. // https://tools.ietf.org/html/rfc4648#section-5
  18. //
  19. // The |policy| defines whether padding should be included or omitted from the
  20. // encoded |*output|. |input| and |*output| may reference the same storage.
  21. BASE_EXPORT void Base64UrlEncode(const StringPiece& input,
  22. Base64UrlEncodePolicy policy,
  23. std::string* output);
  24. enum class Base64UrlDecodePolicy {
  25. // Require inputs contain trailing padding if non-aligned.
  26. REQUIRE_PADDING,
  27. // Accept inputs regardless of whether or not they have the correct padding.
  28. IGNORE_PADDING,
  29. // Reject inputs if they contain any trailing padding.
  30. DISALLOW_PADDING
  31. };
  32. // Decodes the |input| string in base64url, defined in RFC 4648:
  33. // https://tools.ietf.org/html/rfc4648#section-5
  34. //
  35. // The |policy| defines whether padding will be required, ignored or disallowed
  36. // altogether. |input| and |*output| may reference the same storage.
  37. [[nodiscard]] BASE_EXPORT bool Base64UrlDecode(const StringPiece& input,
  38. Base64UrlDecodePolicy policy,
  39. std::string* output);
  40. } // namespace base
  41. #endif // BASE_BASE64URL_H_