OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/proximity_auth/cryptauth/base64url.h" | 5 #include "components/proximity_auth/cryptauth/base64url.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 | 9 |
10 namespace proximity_auth { | 10 namespace proximity_auth { |
11 | 11 |
12 void Base64UrlEncode(const std::string& decoded_input, | 12 void Base64UrlEncode(const std::string& decoded_input, |
13 std::string* encoded_output) { | 13 std::string* encoded_output) { |
14 base::Base64Encode(decoded_input, encoded_output); | 14 base::Base64Encode(decoded_input, encoded_output); |
15 base::ReplaceChars(*encoded_output, "+", "-", encoded_output); | 15 base::ReplaceChars(*encoded_output, "+", "-", encoded_output); |
16 base::ReplaceChars(*encoded_output, "/", "_", encoded_output); | 16 base::ReplaceChars(*encoded_output, "/", "_", encoded_output); |
17 } | 17 } |
18 | 18 |
19 bool Base64UrlDecode(const std::string& encoded_input, | 19 bool Base64UrlDecode(const std::string& encoded_input, |
20 std::string* decoded_output) { | 20 std::string* decoded_output) { |
| 21 // Bail on malformed strings, which already contain a '+' or a '/'. All valid |
| 22 // strings should escape these special characters as '-' and '_', |
| 23 // respectively. |
| 24 if (encoded_input.find_first_of("+/") != std::string::npos) |
| 25 return false; |
| 26 |
21 std::string adjusted_encoded_input = encoded_input; | 27 std::string adjusted_encoded_input = encoded_input; |
22 base::ReplaceChars(adjusted_encoded_input, "-", "+", &adjusted_encoded_input); | 28 base::ReplaceChars(adjusted_encoded_input, "-", "+", &adjusted_encoded_input); |
23 base::ReplaceChars(adjusted_encoded_input, "_", "/", &adjusted_encoded_input); | 29 base::ReplaceChars(adjusted_encoded_input, "_", "/", &adjusted_encoded_input); |
24 | 30 |
25 return base::Base64Decode(adjusted_encoded_input, decoded_output); | 31 return base::Base64Decode(adjusted_encoded_input, decoded_output); |
26 } | 32 } |
27 | 33 |
28 } // namespace proximity_auth | 34 } // namespace proximity_auth |
OLD | NEW |