Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the Chrome Extension ID of an unpacked extension generated?

I would like to share an unpacked extension with my colleagues. It uses the method chrome.runtime.sendMessage(string extensionId, any message, object options, function responseCallback) in an injected script. For that I need to know the extension ID in advance.

Will the extension ID of the unpacked extension will be different on different systems or can I hard code the one I have found in my extension menu?

like image 385
mritz_p Avatar asked Sep 26 '14 06:09

mritz_p


People also ask

What is an unpacked Chrome extension?

Chrome extensions can be either packed or unpacked. Packed extensions are a single file with a . crx extension. Unpacked extensions are a directory containing the extension, including a manifest. json file.

How do I change my Chrome extension ID?

Once uploaded to the Chrome Web Store, your extension ID is fixed and cannot be changed any more. The ID is derived from the . pem file that was created the first time you (or the Chrome Web Store) packed the extension in a .

Where does Chrome store extension information?

When extensions are installed into Chrome they are extracted into the C:\Users\[login_name]\AppData\Local\Google\Chrome\User Data\Default\Extensions folder. Each extension will be stored in its own folder named after the ID of the extension.


1 Answers

While I linked to this question that explains how to "pin" an ID for an unpacked extension, which would solve the practical problem OP faces, the question itself (as stated in the title) is interesting.

If we look at the Chromium source, we will see that the ID is simply a SHA hash of a (maybe-normalized, whatever that means) absolute path to the extension. Highlights from the code:

// 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.

// chromium/src/chrome/browser/extensions/unpacked_installer.cc
int UnpackedInstaller::GetFlags() {
  std::string id = crx_file::id_util::GenerateIdForPath(extension_path_);
  /* ... */
}

// chromium/src/components/crx_file/id_util.cc
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);
}

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;
}

As such, it should ONLY depend on the absolute filesystem path to the extension folder.

like image 102
Xan Avatar answered Oct 24 '22 22:10

Xan