Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
bytearray speedup - SecurityInnovation#87
Browse files Browse the repository at this point in the history
  • Loading branch information
Commod0re committed Jul 31, 2014
1 parent f6ac1be commit 6b9cd4f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
9 changes: 9 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
Changelog
*********

v0.2.2
======

PGPy v0.2.2 is primarily a bugfix release.

Bugs Fixed
----------
* Switched the main parse loop to use a bytearray instead of slicing a bytes, resulting in a ~160x speedup in parsing large blocks of pasing.

v0.2.1
======

Expand Down
4 changes: 2 additions & 2 deletions pgpy/packet/fields/keyfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def parse(self, packet, pktype, alg, sec=False):
mend = pos + bytelen

getattr(self, field)['bitlen'] = bitlen
getattr(self, field)['bytes'] = packet[pos:mend]
getattr(self, field)['bytes'] = bytes(packet[pos:mend])

pos = mend

Expand Down Expand Up @@ -268,7 +268,7 @@ def parse(self, packet):
pos = 13

if self.id != 0:
self.iv = packet[pos:(pos + int(self.alg.block_size / 8))]
self.iv = bytes(packet[pos:(pos + int(self.alg.block_size / 8))])

def derive_key(self, passphrase):
# we use the fields stored here along with the RFC 4880 String-to-Key usage description
Expand Down
2 changes: 1 addition & 1 deletion pgpy/packet/packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def parse(self, packet):
mend = -2
if self.stokey.id == 254:
mend = len(packet)
self.enc_seckey_material = packet[pos:mend]
self.enc_seckey_material = bytes(packet[pos:mend])

if self.stokey.id in [0, 255]:
self.checksum = packet[pos:]
Expand Down
13 changes: 6 additions & 7 deletions pgpy/pgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def parse(self):
for key, val in [ (h[i], h[i + 1]) for i in range(0, len(h), 2) ]:
self.ascii_headers[key] = val

self.data = base64.b64decode(k[2].replace('\n', '').encode())
self.data = bytearray(base64.b64decode(k[2].replace('\n', '').encode()))
self.crc = bytes_to_int(base64.b64decode(k[3].encode()))

# verify CRC
Expand All @@ -169,10 +169,9 @@ def parse(self):

# dump fields in all contained packets per RFC 4880, without using pgpdump
if self.data != b'':
pos = 0
while pos < len(self.data):
pkt = Packet(self.data[pos:])
pos += len(pkt.header.__bytes__()) + pkt.header.length
while len(self.data) > 0:
pkt = Packet(self.data)
del self.data[:(len(pkt.header.__bytes__()) + pkt.header.length)]
self.packets.append(pkt)

def crc24(self):
Expand All @@ -185,7 +184,7 @@ def crc24(self):
# The accumulation is done on the data before it is converted to
# radix-64, rather than on the converted data.
if self.data == b'':
self.data = self.__bytes__()
self.data = bytearray(self.__bytes__())

crc = self.crc24_init
sig = [ ord(i) for i in self.data ] if type(self.data) is str else self.data
Expand All @@ -210,7 +209,7 @@ def extract_pgp_ascii_block(self):
# this is binary data; skip extracting the block and move on
else:
self.bytes = b''
self.data = data
self.data = bytearray(data)
return

# are there any ASCII armored PGP blocks present? if not, we may be dealing with binary data instead
Expand Down

0 comments on commit 6b9cd4f

Please sign in to comment.