base64url.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/compiler_specific.h"
  9. #include "base/macros.h"
  10. #include "base/strings/string_piece.h"
  11. namespace base {
  12. enum class Base64UrlEncodePolicy {
  13. // Include the trailing padding in the output, when necessary.
  14. INCLUDE_PADDING,
  15. // Remove the trailing padding from the output.
  16. OMIT_PADDING
  17. };
  18. // Encodes the |input| string in base64url, defined in RFC 4648:
  19. // https://tools.ietf.org/html/rfc4648#section-5
  20. //
  21. // The |policy| defines whether padding should be included or omitted from the
  22. // encoded |*output|. |input| and |*output| may reference the same storage.
  23. BASE_EXPORT void Base64UrlEncode(const StringPiece& input,
  24. Base64UrlEncodePolicy policy,
  25. std::string* output);
  26. enum class Base64UrlDecodePolicy {
  27. // Require inputs contain trailing padding if non-aligned.
  28. REQUIRE_PADDING,
  29. // Accept inputs regardless of whether or not they have the correct padding.
  30. IGNORE_PADDING,
  31. // Reject inputs if they contain any trailing padding.
  32. DISALLOW_PADDING
  33. };
  34. // Decodes the |input| string in base64url, defined in RFC 4648:
  35. // https://tools.ietf.org/html/rfc4648#section-5
  36. //
  37. // The |policy| defines whether padding will be required, ignored or disallowed
  38. // altogether. |input| and |*output| may reference the same storage.
  39. BASE_EXPORT bool Base64UrlDecode(const StringPiece& input,
  40. Base64UrlDecodePolicy policy,
  41. std::string* output) WARN_UNUSED_RESULT;
  42. } // namespace base
  43. #endif // BASE_BASE64URL_H_