12

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?

3
  • 1
    possible duplicate of Obtaining Chrome Extension ID for development
    – Xan
    Sep 26, 2014 at 7:33
  • 1
    That said, do you even need that? Do you mean, by injected script, injected in the page's content?
    – Xan
    Sep 26, 2014 at 7:35
  • I inject the script by executing the following JavaScript: var s = document.createElement("script"); s.src = chrome.extension.getURL("api.js"); because I need the script to access the window object of any page it is injected to.
    – mritz_p
    Sep 26, 2014 at 7:48

3 Answers 3

25

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.

0
3

Xan's answer is correct. However, just to extend it for the reference based on Chromium code above one could calculate extension id with this python code.

import hashlib

m = hashlib.sha256()
m.update(bytes(PATH.encode('utf-8')))
EXTID = ''.join([chr(int(i, base=16) + ord('a')) for i in m.hexdigest()][:32])
3
  • 1
    On Windows, the path needs to be encoded as utf-16-le. May 26, 2021 at 9:42
  • Thanks but how can you do this in c#? Jul 21, 2021 at 9:23
  • or import hashlib, string; PATH=""; print(hashlib.sha256(PATH.encode('utf8')).hexdigest()[:32].translate(str.maketrans(string.hexdigits[:16], string.ascii_lowercase[:16]))) - see also: Caesar Cipher Function in Python
    – milahu
    Nov 24, 2023 at 17:34
0

MaybeNormalizePath is affecting Windows usecase:

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
}



https://chromium.googlesource.com/chromium/chromium/+/refs/heads/trunk/extensions/common/id_util.cc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.