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/crx_file/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/sha1.h" |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "crypto/sha2.h" | 11 #include "crypto/sha2.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 // Converts a normal hexadecimal string into the alphabet used by extensions. | 15 // 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 | 16 // 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 | 17 // completely numeric host, since some software interprets that as an IP |
17 // address. | 18 // address. |
(...skipping 28 matching lines...) Expand all Loading... |
46 } | 47 } |
47 | 48 |
48 std::string GenerateIdForPath(const base::FilePath& path) { | 49 std::string GenerateIdForPath(const base::FilePath& path) { |
49 base::FilePath new_path = MaybeNormalizePath(path); | 50 base::FilePath new_path = MaybeNormalizePath(path); |
50 std::string path_bytes = | 51 std::string path_bytes = |
51 std::string(reinterpret_cast<const char*>(new_path.value().data()), | 52 std::string(reinterpret_cast<const char*>(new_path.value().data()), |
52 new_path.value().size() * sizeof(base::FilePath::CharType)); | 53 new_path.value().size() * sizeof(base::FilePath::CharType)); |
53 return GenerateId(path_bytes); | 54 return GenerateId(path_bytes); |
54 } | 55 } |
55 | 56 |
| 57 std::string HashedIdInHex(const std::string& id) { |
| 58 const std::string id_hash = base::SHA1HashString(id); |
| 59 DCHECK_EQ(base::kSHA1Length, id_hash.length()); |
| 60 return base::HexEncode(id_hash.c_str(), id_hash.length()); |
| 61 } |
| 62 |
56 base::FilePath MaybeNormalizePath(const base::FilePath& path) { | 63 base::FilePath MaybeNormalizePath(const base::FilePath& path) { |
57 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
58 // Normalize any drive letter to upper-case. We do this for consistency with | 65 // Normalize any drive letter to upper-case. We do this for consistency with |
59 // net_utils::FilePathToFileURL(), which does the same thing, to make string | 66 // net_utils::FilePathToFileURL(), which does the same thing, to make string |
60 // comparisons simpler. | 67 // comparisons simpler. |
61 base::FilePath::StringType path_str = path.value(); | 68 base::FilePath::StringType path_str = path.value(); |
62 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && | 69 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && |
63 path_str[1] == L':') | 70 path_str[1] == L':') |
64 path_str[0] = towupper(path_str[0]); | 71 path_str[0] = towupper(path_str[0]); |
65 | 72 |
(...skipping 13 matching lines...) Expand all Loading... |
79 std::string temp = base::StringToLowerASCII(id); | 86 std::string temp = base::StringToLowerASCII(id); |
80 for (size_t i = 0; i < temp.size(); i++) | 87 for (size_t i = 0; i < temp.size(); i++) |
81 if (temp[i] < 'a' || temp[i] > 'p') | 88 if (temp[i] < 'a' || temp[i] > 'p') |
82 return false; | 89 return false; |
83 | 90 |
84 return true; | 91 return true; |
85 } | 92 } |
86 | 93 |
87 } // namespace id_util | 94 } // namespace id_util |
88 } // namespace crx_file | 95 } // namespace crx_file |
OLD | NEW |