[pycrypto] ERROR: testRsaUnversionedSignAndVerify failed

Dwayne C. Litzenberger dlitz at dlitz.net
Fri Aug 28 10:33:05 CST 2009


On Thu, Aug 20, 2009 at 09:55:43PM -0400, Dwayne C. Litzenberger wrote:
>You can use this code.  I hereby release it into the public domain:
>
>try:
>    from Crypto import Random
>    _rng_instance = Random.new()
>except ImportError:
>    class RandomStub:
>        def read(n):
>            return os.urandom(n)
>    _rng_instance = RandomStub()
>
>def RandBytes(n):
>    """Return n random bytes."""
>    return _rng_instance.read(n)

Correction: You need not create a global _rng_instance singleton, since 
Random.new() already does this for you.  Use this code instead:

     # Define the RandBytes function using one of the following
     # random sources (in preferential order):
     # 1. Crypto.Random
     # 2. os.urandom
     try:
          from Crypto.Random import get_random_bytes as RandBytes
     except ImportError:
          from os import urandom as RandBytes

If you want support for versions of Python prior to 2.4 on Unix, you can do 
this:

     # Define the RandBytes function using one of the following
     # random sources (in preferential order):
     # 1. Crypto.Random
     # 2. os.urandom
     # 3. /dev/urandom
     try:
          from Crypto.Random import get_random_bytes as RandBytes
     except ImportError:
          try:
              from os import urandom as RandBytes
          except ImportError:
              RandBytes = open("/dev/urandom", "rb").read

In any case, RandomPool should not be used at all, ever.

The code above is released into the public domain (copyright abandoned).

-- 
Dwayne C. Litzenberger <dlitz at dlitz.net>
   Key-signing key   - 19E1 1FE8 B3CF F273 ED17  4A24 928C EC13 39C2 5CF7


More information about the pycrypto mailing list