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.
5 #include "xwalk/application/common/id_util.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "crypto/sha2.h"
11 #include "xwalk/application/common/application_manifest_constants.h"
12 #include "xwalk/application/common/manifest_handlers/tizen_application_handler.h"
15 #include "third_party/re2/re2/re2.h"
19 namespace application {
22 const char kTizenAppIdPattern[] = "\\A[0-9a-zA-Z]{10}[.][0-9a-zA-Z]{1,52}\\z";
23 const std::string kAppIdPrefix("xwalk.");
27 // Converts a normal hexadecimal string into the alphabet used by applications.
28 // We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a
29 // completely numeric host, since some software interprets that as an IP
31 static void ConvertHexadecimalToIDAlphabet(std::string* id) {
32 for (size_t i = 0; i < id->size(); ++i) {
34 if (base::HexStringToInt(base::StringPiece(
35 id->begin() + i, id->begin() + i + 1), &val)) {
43 // First 16 bytes of SHA256 hashed public key.
44 const size_t kIdSize = 16;
47 const size_t kLegacyTizenIdSize = 10;
50 std::string GenerateId(const std::string& input) {
52 crypto::SHA256HashString(input, hash, sizeof(hash));
53 std::string output = StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
54 ConvertHexadecimalToIDAlphabet(&output);
59 std::string GenerateIdForPath(const base::FilePath& path) {
60 std::string path_bytes =
61 std::string(reinterpret_cast<const char*>(path.value().data()),
62 path.value().size() * sizeof(base::FilePath::CharType));
63 return GenerateId(path_bytes);
67 std::string RawAppIdToCrosswalkAppId(const std::string& id) {
68 if (RE2::PartialMatch(id, kTizenAppIdPattern))
69 return GenerateId(id);
73 std::string RawAppIdToAppIdForTizenPkgmgrDB(const std::string& id) {
74 if (RE2::PartialMatch(id, kTizenAppIdPattern))
76 return kAppIdPrefix + id;
79 std::string TizenPkgmgrDBAppIdToRawAppId(const std::string& id) {
81 if (RE2::FullMatch(id, "xwalk.(\\w+)", &raw_id))
86 std::string GetTizenAppId(ApplicationData* application) {
87 if (application->GetPackageType() == xwalk::application::Package::XPK)
88 return application->ID();
90 const TizenApplicationInfo* tizen_app_info =
91 static_cast<TizenApplicationInfo*>(application->GetManifestData(
92 application_widget_keys::kTizenApplicationKey));
93 return tizen_app_info->id();
97 } // namespace application