2

Is Python's 3 random.random() good enough for security if seeded with high entropy seeds before every use? How to know and verify beyond verifying the distribution shape.

So the question is about using random.random to transform some entropy source to many choices inside of an item of an array.

Let say for example we have a dictionary of 9k words and want 18 words chosen at random. We do not want anyone else to ever be able to find them by playing around with pythons' random.random().

dic = ["word1", "word2", ... , "word19k"]
while(true):
  seed = os.urandom(50)
  random.seed(seed)
  print(random.choice(dic))
PyWebDesign
  • 187
  • 1
  • 11
  • Possible duplicate of [How can I create a random number that is cryptographically secure in python?](https://stackoverflow.com/questions/20936993/how-can-i-create-a-random-number-that-is-cryptographically-secure-in-python) – cs95 Dec 12 '17 at 12:52
  • Thx COLDSPEED, that is not what I am searching for even though I understand it is a better solution. I do have a system that would cost a lot to change and use very high entropy seed to seed random.random() so I need to assess security before making a choice. – PyWebDesign Dec 12 '17 at 12:57
  • 2
    Depends on what you mean by "high entropy seeds." If the entropy is so high that the seeds are truly random (and drawn from a large range) then of course it would be secure, but at that stage the Python random number generator becomes a superfluous middleman. – John Coleman Dec 12 '17 at 16:25

2 Answers2

3

Python documentation of random specifies:

Warning The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

As stated in the comments of the question, the better and more secure way of generating random numbers (for cryptography purposes) is os.urandom().

Example code from the linked question

>>> import os
>>> os.urandom(10)
'm\xd4\x94\x00x7\xbe\x04\xa2R'
>>> type(os.urandom(10))
<type 'str'>
>>> map(ord, os.urandom(10))
[65, 120, 218, 135, 66, 134, 141, 140, 178, 25]
L_Church
  • 703
  • 1
  • 8
  • 19
  • Hi thank you very much, this is not exactly what I need since I do understand quite well that there are better methods available. I have modified the question code example to introduce your solution as the seed of random.random(). I do have a system that would cost a lot to change and use very high entropy seed to seed random.random() so I need to assess security before making a choice. – PyWebDesign Dec 12 '17 at 13:08
  • 1
    When poor choices are made there is a cost to correct them, pay the cost. – zaph Dec 12 '17 at 13:52
2

Ok so after asking elsewhere, if the random generator produce a uniform distribution, the whole operation is useless but does not introduce security risks.

as in the question, if the random generator is seeded with new seed for every generation. It is like applying a simple useless transformation x => f(x).

The response to this question is very simple if you use high entropy and pass it through python random.random ou random.choice, it is as secure as the seed quality. I would not recommend changing this as a high priority change to a running system.

The other part of the response is: don't do that, it is useless. Use a better way to secure pick instead of random.choice.

PyWebDesign
  • 187
  • 1
  • 11