Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(481)

Side by Side Diff: extensions/common/id_util.cc

Issue 481433005: Extensions: Move id_util functions to crx_file component. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: revert last patchset. function returns Extension* and can't use an assert. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « extensions/common/id_util.h ('k') | extensions/common/id_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "extensions/common/id_util.h"
6
7 #include "base/files/file_path.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "crypto/sha2.h"
11
12 namespace {
13
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
16 // completely numeric host, since some software interprets that as an IP
17 // address.
18 static void ConvertHexadecimalToIDAlphabet(std::string* id) {
19 for (size_t i = 0; i < id->size(); ++i) {
20 int val;
21 if (base::HexStringToInt(base::StringPiece(id->begin() + i,
22 id->begin() + i + 1),
23 &val)) {
24 (*id)[i] = val + 'a';
25 } else {
26 (*id)[i] = 'a';
27 }
28 }
29 }
30
31 } // namespace
32
33 namespace extensions {
34 namespace id_util {
35
36 // First 16 bytes of SHA256 hashed public key.
37 const size_t kIdSize = 16;
38
39 std::string GenerateId(const std::string& input) {
40 uint8 hash[kIdSize];
41 crypto::SHA256HashString(input, hash, sizeof(hash));
42 std::string output =
43 base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
44 ConvertHexadecimalToIDAlphabet(&output);
45
46 return output;
47 }
48
49 std::string GenerateIdForPath(const base::FilePath& path) {
50 base::FilePath new_path = MaybeNormalizePath(path);
51 std::string path_bytes =
52 std::string(reinterpret_cast<const char*>(new_path.value().data()),
53 new_path.value().size() * sizeof(base::FilePath::CharType));
54 return GenerateId(path_bytes);
55 }
56
57 base::FilePath MaybeNormalizePath(const base::FilePath& path) {
58 #if defined(OS_WIN)
59 // Normalize any drive letter to upper-case. We do this for consistency with
60 // net_utils::FilePathToFileURL(), which does the same thing, to make string
61 // comparisons simpler.
62 base::FilePath::StringType path_str = path.value();
63 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' &&
64 path_str[1] == L':')
65 path_str[0] = towupper(path_str[0]);
66
67 return base::FilePath(path_str);
68 #else
69 return path;
70 #endif
71 }
72
73 } // namespace id_util
74 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/common/id_util.h ('k') | extensions/common/id_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698