lundi 8 mars 2021

Cryptographically secure pseudo random shuffle a list or array in python

I am in need of a shuffle function that uses CSPRNG (Cryptographically Secure Pseudo Random Number Generator) and can be seeded manually for the same output for the same seed.

The built-in random.shuffle() in Python can be manually seeded but is not suitable for cryptographic use and will be removed in version 3.11 of python.

Crypto.Random.random.shuffle() from PyCryptodome does not accept a seed as far as I can gather.

Currently, I have settled for the Mersenne Twister used by the built-in random.shuffle() function to shuffle a list of numbers but this is far from ideal. Shuffling a numpy array is also welcome as the built-in numpy.random.shuffle is not suitable for cryptographic purposes. The array/list may have elements upwards of 10 billion so performance and randomness are key.

The bandaid code is below.

import numpy as np
from random import seed, shuffle
array = np.arange(10) # [0 1 2 3 4 5 6 7 8 9]
print(array)
seed(1)
shuffle(array)
print(array) # [6 8 9 7 5 3 0 4 1 2]



Aucun commentaire:

Enregistrer un commentaire