[pycrypto] Inovking Depreciation warnings in Python

Dwayne Litzenberger dlitz at dlitz.net
Thu Dec 6 14:16:12 PST 2012


On Thu, Dec 06, 2012 at 04:23:23PM +0100, Oz Nahum Tiram wrote:
>Should, I worry about using RandomPool?
>Shall I used the class which is wrapped by RandomPool directly?
>
>How do I proceed in the safest way?

Just use the Crypto.Random API.  If you need random bytes, do:

     from Crypto.Random import get_random_bytes

     rndbytes = get_random_bytes(32)     # returns random 256-bit string

You can also get a file-like object from Crypto.Random:

     from Crypto import Random

     f = Random.new()
     f.read(32)              # 32 random bytes
     f.read(32)              # another 32 random bytes

We also provide a stdlib-like "random" module:

     from Crypto.Random import random

     # print a random lowercase letter
     print random.choice("abcdefghijklmnopqrstuvwxyz")

Of course, you can also just use os.urandom:

     import os

     rndbytes = os.urandom(32)       # returns random 256-bit string

Cheers,
- Dwayne

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


More information about the pycrypto mailing list