Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: packagerChrome.py

Issue 29349869: Issue 4339 - Replace M2Crypto by PyCrypto (Closed)
Patch Set: Addressed comments Created Aug. 17, 2016, 10:02 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | packagerSafari.py » ('j') | packagerSafari.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # This Source Code Form is subject to the terms of the Mozilla Public 1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 4
5 import sys 5 import errno
6 import io
7 import json
6 import os 8 import os
7 import re 9 import re
8 import json 10 from StringIO import StringIO
9 import struct 11 import struct
10 import io 12 import sys
11 from StringIO import StringIO
12 13
13 import packager 14 import packager
14 from packager import readMetadata, getMetadataPath, getDefaultFileName, getBuild Version, getTemplate, Files 15 from packager import readMetadata, getMetadataPath, getDefaultFileName, getBuild Version, getTemplate, Files
15 16
16 defaultLocale = 'en_US' 17 defaultLocale = 'en_US'
17 18
18 19
19 def getIgnoredFiles(params): 20 def getIgnoredFiles(params):
20 return {'store.description'} 21 return {'store.description'}
21 22
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 data = json.loads(files[filename]) 289 data = json.loads(files[filename])
289 for name, info in defaults.iteritems(): 290 for name, info in defaults.iteritems():
290 data.setdefault(name, info) 291 data.setdefault(name, info)
291 for name, limit in limits.iteritems(): 292 for name, limit in limits.iteritems():
292 if name in data: 293 if name in data:
293 data[name]['message'] = truncate(data[name]['message'], limit) 294 data[name]['message'] = truncate(data[name]['message'], limit)
294 files[filename] = toJson(data) 295 files[filename] = toJson(data)
295 296
296 297
297 def signBinary(zipdata, keyFile): 298 def signBinary(zipdata, keyFile):
298 import M2Crypto 299 from Crypto.Hash import SHA
299 if not os.path.exists(keyFile): 300 from Crypto.PublicKey import RSA
300 M2Crypto.RSA.gen_key(1024, 65537, callback=lambda x: None).save_key(keyF ile, cipher=None) 301 from Crypto.Signature import PKCS1_v1_5
301 key = M2Crypto.EVP.load_key(keyFile) 302
302 key.sign_init() 303 try:
303 key.sign_update(zipdata) 304 with open(keyFile, 'rb') as file:
304 return key.final() 305 key = RSA.importKey(file.read())
306 except IOError as e:
307 if e.errno != errno.ENOENT:
308 raise
309 key = RSA.generate(2048)
310 with open(keyFile, 'wb') as file:
311 file.write(key.exportKey('PEM'))
312
313 return PKCS1_v1_5.new(key).sign(SHA.new(zipdata))
305 314
306 315
307 def getPublicKey(keyFile): 316 def getPublicKey(keyFile):
308 import M2Crypto 317 from Crypto.PublicKey import RSA
309 return M2Crypto.EVP.load_key(keyFile).as_der() 318 with open(keyFile, 'rb') as file:
319 return RSA.importKey(file.read()).publickey().exportKey('DER')
310 320
311 321
312 def writePackage(outputFile, pubkey, signature, zipdata): 322 def writePackage(outputFile, pubkey, signature, zipdata):
313 if isinstance(outputFile, basestring): 323 if isinstance(outputFile, basestring):
314 file = open(outputFile, 'wb') 324 file = open(outputFile, 'wb')
315 else: 325 else:
316 file = outputFile 326 file = outputFile
317 if pubkey != None and signature != None: 327 if pubkey != None and signature != None:
318 file.write(struct.pack('<4sIII', 'Cr24', 2, len(pubkey), len(signature)) ) 328 file.write(struct.pack('<4sIII', 'Cr24', 2, len(pubkey), len(signature)) )
319 file.write(pubkey) 329 file.write(pubkey)
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 files['qunit/index.html'] = createScriptPage(params, 'testIndex.html.tmp l', 385 files['qunit/index.html'] = createScriptPage(params, 'testIndex.html.tmp l',
376 ('general', 'testScripts')) 386 ('general', 'testScripts'))
377 387
378 zipdata = files.zipToString() 388 zipdata = files.zipToString()
379 signature = None 389 signature = None
380 pubkey = None 390 pubkey = None
381 if keyFile != None: 391 if keyFile != None:
382 signature = signBinary(zipdata, keyFile) 392 signature = signBinary(zipdata, keyFile)
383 pubkey = getPublicKey(keyFile) 393 pubkey = getPublicKey(keyFile)
384 writePackage(outFile, pubkey, signature, zipdata) 394 writePackage(outFile, pubkey, signature, zipdata)
OLDNEW
« no previous file with comments | « no previous file | packagerSafari.py » ('j') | packagerSafari.py » ('J')

Powered by Google App Engine
This is Rietveld