OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "extensions/common/id_util.h" | 5 #include "components/crx_file/id_util.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "crypto/sha2.h" | 10 #include "crypto/sha2.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 // Converts a normal hexadecimal string into the alphabet used by extensions. | 14 // Converts a normal hexadecimal string into the alphabet used by extensions. |
15 // We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a | 15 // We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a |
16 // completely numeric host, since some software interprets that as an IP | 16 // completely numeric host, since some software interprets that as an IP |
17 // address. | 17 // address. |
18 static void ConvertHexadecimalToIDAlphabet(std::string* id) { | 18 static void ConvertHexadecimalToIDAlphabet(std::string* id) { |
19 for (size_t i = 0; i < id->size(); ++i) { | 19 for (size_t i = 0; i < id->size(); ++i) { |
20 int val; | 20 int val; |
21 if (base::HexStringToInt(base::StringPiece(id->begin() + i, | 21 if (base::HexStringToInt( |
22 id->begin() + i + 1), | 22 base::StringPiece(id->begin() + i, id->begin() + i + 1), &val)) { |
23 &val)) { | |
24 (*id)[i] = val + 'a'; | 23 (*id)[i] = val + 'a'; |
25 } else { | 24 } else { |
26 (*id)[i] = 'a'; | 25 (*id)[i] = 'a'; |
27 } | 26 } |
28 } | 27 } |
29 } | 28 } |
30 | 29 |
31 } // namespace | 30 } // namespace |
32 | 31 |
33 namespace extensions { | 32 namespace crx_file { |
34 namespace id_util { | 33 namespace id_util { |
35 | 34 |
36 // First 16 bytes of SHA256 hashed public key. | 35 // First 16 bytes of SHA256 hashed public key. |
37 const size_t kIdSize = 16; | 36 const size_t kIdSize = 16; |
38 | 37 |
39 std::string GenerateId(const std::string& input) { | 38 std::string GenerateId(const std::string& input) { |
40 uint8 hash[kIdSize]; | 39 uint8 hash[kIdSize]; |
41 crypto::SHA256HashString(input, hash, sizeof(hash)); | 40 crypto::SHA256HashString(input, hash, sizeof(hash)); |
42 std::string output = | 41 std::string output = |
43 base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash))); | 42 base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash))); |
(...skipping 19 matching lines...) Expand all Loading... |
63 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && | 62 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && |
64 path_str[1] == L':') | 63 path_str[1] == L':') |
65 path_str[0] = towupper(path_str[0]); | 64 path_str[0] = towupper(path_str[0]); |
66 | 65 |
67 return base::FilePath(path_str); | 66 return base::FilePath(path_str); |
68 #else | 67 #else |
69 return path; | 68 return path; |
70 #endif | 69 #endif |
71 } | 70 } |
72 | 71 |
| 72 bool IdIsValid(const std::string& id) { |
| 73 // Verify that the id is legal. |
| 74 if (id.size() != (crx_file::id_util::kIdSize * 2)) |
| 75 return false; |
| 76 |
| 77 // We only support lowercase IDs, because IDs can be used as URL components |
| 78 // (where GURL will lowercase it). |
| 79 std::string temp = base::StringToLowerASCII(id); |
| 80 for (size_t i = 0; i < temp.size(); i++) |
| 81 if (temp[i] < 'a' || temp[i] > 'p') |
| 82 return false; |
| 83 |
| 84 return true; |
| 85 } |
| 86 |
73 } // namespace id_util | 87 } // namespace id_util |
74 } // namespace extensions | 88 } // namespace crx_file |
OLD | NEW |