Remove implicit conversions from scoped_refptr to T* in net/socket/
[chromium-blink-merge.git] / components / crx_file / id_util.cc
blobef271ad1456a2ffa8b2405f87e3aa073a419dbb9
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
3 // found in the LICENSE file.
5 #include "components/crx_file/id_util.h"
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"
12 namespace {
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(
22 base::StringPiece(id->begin() + i, id->begin() + i + 1), &val)) {
23 (*id)[i] = val + 'a';
24 } else {
25 (*id)[i] = 'a';
30 } // namespace
32 namespace crx_file {
33 namespace id_util {
35 // First 16 bytes of SHA256 hashed public key.
36 const size_t kIdSize = 16;
38 std::string GenerateId(const std::string& input) {
39 uint8 hash[kIdSize];
40 crypto::SHA256HashString(input, hash, sizeof(hash));
41 std::string output =
42 base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
43 ConvertHexadecimalToIDAlphabet(&output);
45 return output;
48 std::string GenerateIdForPath(const base::FilePath& path) {
49 base::FilePath new_path = MaybeNormalizePath(path);
50 std::string path_bytes =
51 std::string(reinterpret_cast<const char*>(new_path.value().data()),
52 new_path.value().size() * sizeof(base::FilePath::CharType));
53 return GenerateId(path_bytes);
56 base::FilePath MaybeNormalizePath(const base::FilePath& path) {
57 #if defined(OS_WIN)
58 // 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
60 // comparisons simpler.
61 base::FilePath::StringType path_str = path.value();
62 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' &&
63 path_str[1] == L':')
64 path_str[0] = towupper(path_str[0]);
66 return base::FilePath(path_str);
67 #else
68 return path;
69 #endif
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;
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;
84 return true;
87 } // namespace id_util
88 } // namespace crx_file