From bda6bc47d71b962bec00142bd5ae5b8b76b142b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Sun, 2 Jun 2013 15:55:23 +0200 Subject: [PATCH] Use buffer instead of string slicing to reduce memory usage. --- darc/key.py | 10 +++++----- darc/remote.py | 4 ++-- darc/test.py | 3 +++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/darc/key.py b/darc/key.py index 1a2ba18b6..700294998 100644 --- a/darc/key.py +++ b/darc/key.py @@ -83,7 +83,7 @@ def encrypt(self, data): def decrypt(self, id, data): if data[0] != self.TYPE: raise IntegrityError('Invalid encryption envelope') - data = zlib.decompress(data[1:]) + data = zlib.decompress(buffer(data, 1)) if id and SHA256.new(data).digest() != id: raise IntegrityError('Chunk id verification failed') return data @@ -107,12 +107,12 @@ def encrypt(self, data): def decrypt(self, id, data): if data[0] != self.TYPE: raise IntegrityError('Invalid encryption envelope') - hash = data[1:33] - if HMAC.new(self.enc_hmac_key, data[33:], SHA256).digest() != hash: + hash = buffer(data, 1, 32) + if buffer(HMAC.new(self.enc_hmac_key, buffer(data, 33), SHA256).digest()) != hash: raise IntegrityError('Encryption envelope checksum mismatch') - nonce = bytes_to_long(data[33:41]) + nonce = bytes_to_long(buffer(data, 33, 8)) counter = Counter.new(64, initial_value=nonce, prefix=PREFIX) - data = zlib.decompress(AES.new(self.enc_key, AES.MODE_CTR, counter=counter).decrypt(data[41:])) + data = zlib.decompress(AES.new(self.enc_key, AES.MODE_CTR, counter=counter).decrypt(buffer(data, 41))) if id and HMAC.new(self.id_key, data, SHA256).digest() != id: raise IntegrityError('Chunk id verification failed') return data diff --git a/darc/remote.py b/darc/remote.py index 3a13e7c23..b5bba1c31 100644 --- a/darc/remote.py +++ b/darc/remote.py @@ -127,7 +127,7 @@ def call(self, cmd, args, wait=True): if to_send: n = os.write(self.stdin_fd, to_send) assert n > 0 - to_send = to_send[n:] + to_send = buffer(to_send, n) else: w_fds = [] @@ -212,7 +212,7 @@ def call_multi(self, cmd, argsv, wait=True, peek=None): if self.to_send: n = os.write(self.stdin_fd, self.to_send) assert n > 0 - self.to_send = self.to_send[n:] + self.to_send = buffer(self.to_send, n) else: w_fds = [] if not wait: diff --git a/darc/test.py b/darc/test.py index bb318ddde..d1a065c25 100644 --- a/darc/test.py +++ b/darc/test.py @@ -92,6 +92,9 @@ def diff_dirs(self, dir1, dir2): attrs.append('st_mtime') d1 = [filename] + [getattr(s1, a) for a in attrs] d2 = [filename] + [getattr(s2, a) for a in attrs] + if(len(d1) == 6): + d1[-1] = int(d1[-1]) + d2[-1] = int(d2[-1]) d1.append(self.get_xattrs(path1)) d2.append(self.get_xattrs(path2)) self.assertEqual(d1, d2)