Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: tools/crx_id/crx_id.py

Issue 16346003: Fix extension id calculation to take into account Windows encoding. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """ Read a CRX file and write out the App ID and the Full Hash of the ID. 6 """ Read a CRX file and write out the App ID and the Full Hash of the ID.
7 See: http://code.google.com/chrome/extensions/crx.html 7 See: http://code.google.com/chrome/extensions/crx.html
8 and 'http://stackoverflow.com/questions/' 8 and 'http://stackoverflow.com/questions/'
9 + '1882981/google-chrome-alphanumeric-hashes-to-identify-extensions' 9 + '1882981/google-chrome-alphanumeric-hashes-to-identify-extensions'
10 for docs on the format. 10 for docs on the format.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 version = f.read(4) 68 version = f.read(4)
69 if not version[0] != EXPECTED_CRX_VERSION: 69 if not version[0] != EXPECTED_CRX_VERSION:
70 raise Exception('Invalid version number: %s (expecting %s)' % 70 raise Exception('Invalid version number: %s (expecting %s)' %
71 (version, 71 (version,
72 EXPECTED_CRX_VERSION)) 72 EXPECTED_CRX_VERSION))
73 pub_key_len_bytes = HexToInt(f.read(4)) 73 pub_key_len_bytes = HexToInt(f.read(4))
74 sig_len_bytes = HexToInt(f.read(4)) 74 sig_len_bytes = HexToInt(f.read(4))
75 pub_key = f.read(pub_key_len_bytes) 75 pub_key = f.read(pub_key_len_bytes)
76 return pub_key 76 return pub_key
77 77
78 def GetPublicKeyFromPath(filepath): 78 def GetPublicKeyFromPath(filepath, encode_path_for_windows=False):
79 # Normalize the path for windows to have capital drive letters. 79 # Normalize the path for windows to have capital drive letters.
80 # We intentionally don't check if sys.platform == 'win32' and just 80 # We intentionally don't check if sys.platform == 'win32' and just
81 # check if this looks like drive letter so that we can test this 81 # check if this looks like drive letter so that we can test this
82 # even on posix systems. 82 # even on posix systems.
83 if (len(filepath) >= 2 and 83 if (len(filepath) >= 2 and
84 filepath[0].islower() and 84 filepath[0].islower() and
85 filepath[1] == ':'): 85 filepath[1] == ':'):
86 return filepath[0].upper() + filepath[1:] 86 filepath = filepath[0].upper() + filepath[1:]
87
88 # On Windows, filepaths are encoded using UTF-16, little endian byte order,
89 # using "wide characters" that are 16 bits in size. On POSIX systems, the
90 # encoding is generally UTF-8, which has the property of being equivalent to
91 # ASCII when only ASCII characters are in the path.
92 if encode_path_for_windows:
93 filepath = filepath.encode('utf-16le')
94
87 return filepath 95 return filepath
88 96
89 def GetPublicKeyUnpacked(f, filepath): 97 def GetPublicKeyUnpacked(f, filepath):
90 manifest = json.load(f) 98 manifest = json.load(f)
91 if 'key' not in manifest: 99 if 'key' not in manifest:
92 # Use the path as the public key. 100 # Use the path as the public key.
93 # See Extension::GenerateIdForPath in extension.cc 101 # See Extension::GenerateIdForPath in extension.cc
94 return GetPublicKeyFromPath(filepath) 102 return GetPublicKeyFromPath(filepath)
95 else: 103 else:
96 return base64.standard_b64decode(manifest['key']) 104 return base64.standard_b64decode(manifest['key'])
97 105
98 def HasPublicKey(filename): 106 def HasPublicKey(filename):
99 if os.path.isdir(filename): 107 if os.path.isdir(filename):
100 with open(os.path.join(filename, 'manifest.json'), 'rb') as f: 108 with open(os.path.join(filename, 'manifest.json'), 'rb') as f:
101 manifest = json.load(f) 109 manifest = json.load(f)
102 return 'key' in manifest 110 return 'key' in manifest
103 return False 111 return False
104 112
105 def GetPublicKey(filename, from_file_path): 113 def GetPublicKey(filename, from_file_path, encode_path_for_windows=False):
achuithb 2013/06/04 18:33:29 encode_path_for_windows seems overly verbose. is_w
Tim Song 2013/06/05 18:36:27 Done.
106 if from_file_path: 114 if from_file_path:
107 return GetPublicKeyFromPath(filename) 115 return GetPublicKeyFromPath(
116 filename, encode_path_for_windows=encode_path_for_windows)
108 117
109 pub_key = '' 118 pub_key = ''
110 if os.path.isdir(filename): 119 if os.path.isdir(filename):
111 # Assume it's an unpacked extension 120 # Assume it's an unpacked extension
112 f = open(os.path.join(filename, 'manifest.json'), 'rb') 121 f = open(os.path.join(filename, 'manifest.json'), 'rb')
113 pub_key = GetPublicKeyUnpacked(f, filename) 122 pub_key = GetPublicKeyUnpacked(f, filename)
114 f.close() 123 f.close()
115 else: 124 else:
116 # Assume it's a packed extension. 125 # Assume it's a packed extension.
117 f = open(filename, 'rb') 126 f = open(filename, 'rb')
118 pub_key = GetPublicKeyPacked(f) 127 pub_key = GetPublicKeyPacked(f)
119 f.close() 128 f.close()
120 return pub_key 129 return pub_key
121 130
122 def GetCRXHash(filename, from_file_path=False): 131 def GetCRXHash(filename, from_file_path=False, encode_path_for_windows=False):
123 pub_key = GetPublicKey(filename, from_file_path) 132 pub_key = GetPublicKey(filename, from_file_path,
133 encode_path_for_windows=encode_path_for_windows)
124 pub_key_hash = hashlib.sha256(pub_key).digest() 134 pub_key_hash = hashlib.sha256(pub_key).digest()
125 return HexTo256(pub_key_hash) 135 return HexTo256(pub_key_hash)
126 136
127 def GetCRXAppID(filename, from_file_path=False): 137 def GetCRXAppID(filename, from_file_path=False, encode_path_for_windows=False):
128 pub_key = GetPublicKey(filename, from_file_path) 138 pub_key = GetPublicKey(filename, from_file_path,
139 encode_path_for_windows=encode_path_for_windows)
129 pub_key_hash = hashlib.sha256(pub_key).digest() 140 pub_key_hash = hashlib.sha256(pub_key).digest()
130 # AppID is the MPDecimal of only the first 128 bits of the hash. 141 # AppID is the MPDecimal of only the first 128 bits of the hash.
131 return HexToMPDecimal(pub_key_hash[:128/8]) 142 return HexToMPDecimal(pub_key_hash[:128/8])
132 143
133 def main(argv): 144 def main(argv):
134 if len(argv) != 2: 145 if len(argv) != 2:
135 usage(argv) 146 usage(argv)
136 return 1 147 return 1
137 print 'Raw Bytes: %s' % GetCRXHash(sys.argv[1]) 148 print 'Raw Bytes: %s' % GetCRXHash(sys.argv[1])
138 print 'AppID: %s' % GetCRXAppID(sys.argv[1]) 149 print 'AppID: %s' % GetCRXAppID(sys.argv[1])
139 150
140 151
141 if __name__ == '__main__': 152 if __name__ == '__main__':
142 sys.exit(main(sys.argv)) 153 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | tools/crx_id/crx_id_unittest.py » ('j') | tools/telemetry/telemetry/core/extension_to_load.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698