crypter performance question

19 views
Skip to first unread message

andres

unread,
Aug 15, 2009, 3:38:26 PM8/15/09
to Keyczar Discuss
Hi,

I'm interested in using keyczar but the Crypter.Encrypt() method takes
about 20 msec to complete (python implementation on a 2.33 GHz mac). I
traced down the bottleneck to line 282 in keyczar.util:

return randpool.RandomPool(515).get_bytes(n)

By bypassing that function call you can improve the speed of the
encryption method by 3 orders of magnitude. Is there a faster method
to use in place of the randpool byte generator? Is it necessary to
supply iv_bytes to AES.new() to begin with?

Thanks,

Andres

Steve Weis

unread,
Aug 15, 2009, 5:51:13 PM8/15/09
to keyczar...@googlegroups.com
Can you measure the performance impact when it uses random.SystemRandom() instead?

andres

unread,
Aug 15, 2009, 8:29:07 PM8/15/09
to Keyczar Discuss
with random.SystemRandom() i'm getting a speed increase of 35x. the
following function:

def RandBytes(n):
"""Return n random bytes."""
gen = random.SystemRandom()
return ''.join([chr(gen.randrange(0,256)) for i in xrange(n)])

takes about 0.7 msec to complete. is this the most optimal way to
generate a random byte string? is there be something wrong with my
installation?

-andres

Thomas Dixon

unread,
Aug 15, 2009, 9:08:20 PM8/15/09
to keyczar...@googlegroups.com
Try this in comparison:

import os, random

def RandBytes(n):
try:
return os.urandom(n)
except (AttributeError, NotImplementedError):
r = random.SystemRandom()
return ''.join([chr(r.randint(0,255)) for i in xrange(n)])

-- Thom
Reply all
Reply to author
Forward
0 new messages