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

Delta Between Two Patch Sets: xarfile.py

Issue 29349885: Issue 4340 - Drop dependency on external xar tool (Closed)
Left Patch Set: Merged cert and private key extraction Created Aug. 17, 2016, 2:10 p.m.
Right Patch Set: Addressed more nits Created Aug. 17, 2016, 7:21 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « packagerSafari.py ('k') | xartoc.xml.tmpl » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 re 5 import re
6 import struct 6 import struct
7 import time 7 import time
8 import zlib 8 import zlib
9 9
10 from Crypto.Hash import SHA 10 from Crypto.Hash import SHA
(...skipping 13 matching lines...) Expand all
24 data = file.read() 24 data = file.read()
25 25
26 certificates = [] 26 certificates = []
27 key = None 27 key = None
28 for match in re.finditer(r'-+BEGIN (.*?)-+(.*?)-+END \1-+', data, re.S): 28 for match in re.finditer(r'-+BEGIN (.*?)-+(.*?)-+END \1-+', data, re.S):
29 section = match.group(1) 29 section = match.group(1)
30 if section == 'CERTIFICATE': 30 if section == 'CERTIFICATE':
31 certificates.append(re.sub(r'\s+', '', match.group(2))) 31 certificates.append(re.sub(r'\s+', '', match.group(2)))
32 elif section == 'PRIVATE KEY': 32 elif section == 'PRIVATE KEY':
33 key = RSA.importKey(match.group(0)) 33 key = RSA.importKey(match.group(0))
34 if key is None: 34 if not key:
Sebastian Noack 2016/08/17 18:41:33 See https://codereview.adblockplus.org/29349869/di
Wladimir Palant 2016/08/17 19:22:28 Done.
35 raise Exception('Cound not find private key in file') 35 raise Exception('Could not find private key in file')
36 36
37 return certificates, key 37 return certificates, key
38 38
39 39
40 def get_checksum(data): 40 def get_checksum(data):
41 return SHA.new(data).digest() 41 return SHA.new(data).digest()
42 42
43 43
44 def get_hexchecksum(data): 44 def get_hexchecksum(data):
45 return SHA.new(data).hexdigest() 45 return SHA.new(data).hexdigest()
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 119
120 offset = params['signature']['offset'] + params['signature']['size'] 120 offset = params['signature']['offset'] + params['signature']['size']
121 compressed_data = compress_files(contents, params['files'], offset) 121 compressed_data = compress_files(contents, params['files'], offset)
122 122
123 template = getTemplate('xartoc.xml.tmpl', autoEscape=True) 123 template = getTemplate('xartoc.xml.tmpl', autoEscape=True)
124 toc_uncompressed = template.render(params).encode('utf-8') 124 toc_uncompressed = template.render(params).encode('utf-8')
125 toc_compressed = zlib.compress(toc_uncompressed, 9) 125 toc_compressed = zlib.compress(toc_uncompressed, 9)
126 126
127 with open(archivepath, 'wb') as file: 127 with open(archivepath, 'wb') as file:
128 # The file starts with a minimalistic header 128 # The file starts with a minimalistic header
129 header = XAR_HEADER.pack(XAR_HEADER_MAGIC, XAR_HEADER.size, 129 file.write(XAR_HEADER.pack(XAR_HEADER_MAGIC, XAR_HEADER.size,
Sebastian Noack 2016/08/17 18:41:34 Nit: I'd personally skip the variable: file.wri
Wladimir Palant 2016/08/17 19:22:28 Done.
130 XAR_VERSION, len(toc_compressed), 130 XAR_VERSION, len(toc_compressed),
131 len(toc_uncompressed), XAR_CKSUM_SHA1) 131 len(toc_uncompressed), XAR_CKSUM_SHA1))
132 file.write(header)
133 132
134 # It's followed up with a compressed XML table of contents 133 # It's followed up with a compressed XML table of contents
135 file.write(toc_compressed) 134 file.write(toc_compressed)
136 135
137 # Now the actual data, all the offsets are in the table of contents 136 # Now the actual data, all the offsets are in the table of contents
138 file.write(get_checksum(toc_compressed)) 137 file.write(get_checksum(toc_compressed))
139 file.write(get_signature(key, toc_compressed)) 138 file.write(get_signature(key, toc_compressed))
140 for blob in compressed_data: 139 for blob in compressed_data:
141 file.write(blob) 140 file.write(blob)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld