Fix AllOrNothing and random.sample()

o AllOrNothing no longer fails occasionally. Patch by Lorenz Quack
o random.sample() works on Python 2.1. Patch by Paul Koning and Lorenz
  Quack
This commit is contained in:
Thorsten Behrens 2011-01-06 07:18:12 -05:00
parent b27696462b
commit 60896cc61a
3 changed files with 7 additions and 9 deletions

View file

@ -190,13 +190,14 @@ class AllOrNothing:
# encrypted, and create the hash cipher.
K0 = self.__K0digit * self.__key_size
hcipher = self.__newcipher(K0)
block_size = self.__ciphermodule.block_size
# Since we have all the blocks (or this method would have been called
# prematurely), we can calcualte all the hash blocks.
# prematurely), we can calculate all the hash blocks.
hashes = []
for i in range(1, len(blocks)):
mticki = blocks[i-1] ^ i
hi = hcipher.encrypt(long_to_bytes(mticki))
hi = hcipher.encrypt(long_to_bytes(mticki, block_size))
hashes.append(bytes_to_long(hi))
# now we can calculate K' (key). remember the last block contains
@ -204,8 +205,7 @@ class AllOrNothing:
key = blocks[-1] ^ reduce(operator.xor, hashes)
# and now we can create the cipher object
mcipher = self.__newcipher(long_to_bytes(key))
block_size = self.__ciphermodule.block_size
mcipher = self.__newcipher(long_to_bytes(key, self.__key_size))
# And we can now decode the original message blocks
parts = []

View file

@ -122,7 +122,7 @@ class StrongRandom(object):
selected = {} # we emulate a set using a dict here
for i in xrange(k):
r = None
while r is None or r in selected:
while r is None or selected.has_key(r):
r = self.randrange(num_choices)
retval.append(population[r])
selected[r] = 1

View file

@ -58,12 +58,10 @@ Use instead assertEqual(expr,True) for assert_ and assertEqual(expr,False) for
failIf
Added unit tests for Crypto.Random.random. Fixed random.shuffle().
Not changed: random.sample() fails on Python 2.1. This is now exposed through
the unit test.
random.sample() changed to no longer fail on Python 2.1.
Added unit test for Crypto.Protocol.AllOrNothing.
Not changed: AllOrNothing fails when called a few times (<10, usually). This
is now exposed through the unit test.
AllOrNothing changed to no longer fail occasionally.
C code: