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

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

Issue 12385037: Add HasPublicKey to crx_id. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: rename from_test_path to from_file_path Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | tools/crx_id/crx_id_unittest.py » ('j') | tools/crx_id/crx_id_unittest.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 88
89 def GetPublicKeyUnpacked(f, filepath): 89 def GetPublicKeyUnpacked(f, filepath):
90 manifest = json.load(f) 90 manifest = json.load(f)
91 if 'key' not in manifest: 91 if 'key' not in manifest:
92 # Use the path as the public key. 92 # Use the path as the public key.
93 # See Extension::GenerateIdForPath in extension.cc 93 # See Extension::GenerateIdForPath in extension.cc
94 return GetPublicKeyFromPath(filepath) 94 return GetPublicKeyFromPath(filepath)
95 else: 95 else:
96 return base64.standard_b64decode(manifest['key']) 96 return base64.standard_b64decode(manifest['key'])
97 97
98 def GetPublicKey(filename, from_test_path): 98 def HasPublicKey(filename):
99 if from_test_path: 99 if os.path.isdir(filename):
100 with open(os.path.join(filename, 'manifest.json'), 'rb') as f:
101 manifest = json.load(f)
102 return 'key' in manifest
103 return False
104
105 def GetPublicKey(filename, from_file_path):
106 if from_file_path:
100 return GetPublicKeyFromPath(filename) 107 return GetPublicKeyFromPath(filename)
101 108
102 pub_key = '' 109 pub_key = ''
103 if os.path.isdir(filename): 110 if os.path.isdir(filename):
104 # Assume it's an unpacked extension 111 # Assume it's an unpacked extension
105 f = open(os.path.join(filename, 'manifest.json'), 'rb') 112 f = open(os.path.join(filename, 'manifest.json'), 'rb')
106 pub_key = GetPublicKeyUnpacked(f, filename) 113 pub_key = GetPublicKeyUnpacked(f, filename)
107 f.close() 114 f.close()
108 else: 115 else:
109 # Assume it's a packed extension. 116 # Assume it's a packed extension.
110 f = open(filename, 'rb') 117 f = open(filename, 'rb')
111 pub_key = GetPublicKeyPacked(f) 118 pub_key = GetPublicKeyPacked(f)
112 f.close() 119 f.close()
113 return pub_key 120 return pub_key
114 121
115 def GetCRXHash(filename, from_test_path=False): 122 def GetCRXHash(filename, from_file_path=False):
116 pub_key = GetPublicKey(filename, from_test_path) 123 pub_key = GetPublicKey(filename, from_file_path)
117 pub_key_hash = hashlib.sha256(pub_key).digest() 124 pub_key_hash = hashlib.sha256(pub_key).digest()
118 return HexTo256(pub_key_hash) 125 return HexTo256(pub_key_hash)
119 126
120 def GetCRXAppID(filename, from_test_path=False): 127 def GetCRXAppID(filename, from_file_path=False):
121 pub_key = GetPublicKey(filename, from_test_path) 128 pub_key = GetPublicKey(filename, from_file_path)
122 pub_key_hash = hashlib.sha256(pub_key).digest() 129 pub_key_hash = hashlib.sha256(pub_key).digest()
123 # AppID is the MPDecimal of only the first 128 bits of the hash. 130 # AppID is the MPDecimal of only the first 128 bits of the hash.
124 return HexToMPDecimal(pub_key_hash[:128/8]) 131 return HexToMPDecimal(pub_key_hash[:128/8])
125 132
126 def main(argv): 133 def main(argv):
127 if len(argv) != 2: 134 if len(argv) != 2:
128 usage(argv) 135 usage(argv)
129 return 1 136 return 1
130 print 'Raw Bytes: %s' % GetCRXHash(sys.argv[1]) 137 print 'Raw Bytes: %s' % GetCRXHash(sys.argv[1])
131 print 'AppID: %s' % GetCRXAppID(sys.argv[1]) 138 print 'AppID: %s' % GetCRXAppID(sys.argv[1])
132 139
133 140
134 if __name__ == '__main__': 141 if __name__ == '__main__':
135 sys.exit(main(sys.argv)) 142 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | tools/crx_id/crx_id_unittest.py » ('j') | tools/crx_id/crx_id_unittest.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698