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