diff options
author | tommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-21 02:27:26 +0000 |
---|---|---|
committer | tommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-21 02:28:55 +0000 |
commit | fdd2837d51c6446e2c417390374fc328a4d25a31 (patch) | |
tree | 4c645e72d2db435562ec275305a2a4b705859204 /components/crx_file/id_util.cc | |
parent | 2bfa5cff6b5c3ab9d6ce4380e435b1006c18458e (diff) | |
download | chromium_src-fdd2837d51c6446e2c417390374fc328a4d25a31.zip chromium_src-fdd2837d51c6446e2c417390374fc328a4d25a31.tar.gz chromium_src-fdd2837d51c6446e2c417390374fc328a4d25a31.tar.bz2 |
Extensions: Move id_util functions to crx_file component.
Consolidates id_util functions of extension/app/component ids in the crx_file component.
It belongs there because these types of ids are not exclusive to extensions, but rather, for anything that comes in a crx file.
BUG=371463
TBR=blundell@chromium.org
Review URL: https://codereview.chromium.org/481433005
Cr-Commit-Position: refs/heads/master@{#290971}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@290971 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/crx_file/id_util.cc')
-rw-r--r-- | components/crx_file/id_util.cc | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/components/crx_file/id_util.cc b/components/crx_file/id_util.cc new file mode 100644 index 0000000..ef271ad --- /dev/null +++ b/components/crx_file/id_util.cc @@ -0,0 +1,88 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/crx_file/id_util.h" + +#include "base/files/file_path.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_util.h" +#include "crypto/sha2.h" + +namespace { + +// Converts a normal hexadecimal string into the alphabet used by extensions. +// We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a +// completely numeric host, since some software interprets that as an IP +// address. +static void ConvertHexadecimalToIDAlphabet(std::string* id) { + for (size_t i = 0; i < id->size(); ++i) { + int val; + if (base::HexStringToInt( + base::StringPiece(id->begin() + i, id->begin() + i + 1), &val)) { + (*id)[i] = val + 'a'; + } else { + (*id)[i] = 'a'; + } + } +} + +} // namespace + +namespace crx_file { +namespace id_util { + +// First 16 bytes of SHA256 hashed public key. +const size_t kIdSize = 16; + +std::string GenerateId(const std::string& input) { + uint8 hash[kIdSize]; + crypto::SHA256HashString(input, hash, sizeof(hash)); + std::string output = + base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash))); + ConvertHexadecimalToIDAlphabet(&output); + + return output; +} + +std::string GenerateIdForPath(const base::FilePath& path) { + base::FilePath new_path = MaybeNormalizePath(path); + std::string path_bytes = + std::string(reinterpret_cast<const char*>(new_path.value().data()), + new_path.value().size() * sizeof(base::FilePath::CharType)); + return GenerateId(path_bytes); +} + +base::FilePath MaybeNormalizePath(const base::FilePath& path) { +#if defined(OS_WIN) + // Normalize any drive letter to upper-case. We do this for consistency with + // net_utils::FilePathToFileURL(), which does the same thing, to make string + // comparisons simpler. + base::FilePath::StringType path_str = path.value(); + if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && + path_str[1] == L':') + path_str[0] = towupper(path_str[0]); + + return base::FilePath(path_str); +#else + return path; +#endif +} + +bool IdIsValid(const std::string& id) { + // Verify that the id is legal. + if (id.size() != (crx_file::id_util::kIdSize * 2)) + return false; + + // We only support lowercase IDs, because IDs can be used as URL components + // (where GURL will lowercase it). + std::string temp = base::StringToLowerASCII(id); + for (size_t i = 0; i < temp.size(); i++) + if (temp[i] < 'a' || temp[i] > 'p') + return false; + + return true; +} + +} // namespace id_util +} // namespace crx_file |