jeudi 16 juin 2016

How can I generate a secure random number of 159bits in php 5.5.9?

I need to do some operations with big numbers in PHP. I had to choose between bcmath and gmp which seem to be the main libraries available for this kind of stuff. I needed the inverse modulo method which is not available in the bcmath library. So I'm using GMP.

Now I need to generate a random secure number between 0 and 2^159-1. But :

  • gmp_random_bits is only available in php >=5.6.3
  • gmp_random_range is only available in php >=5.6.3
  • gmp_random doesn't let me specify precisely the number of bit (and what is a bit per lamb and who works with this kind of unit ?)

I tried to generate the number with openssl_random_pseudo_bytes. I can only use bytes count, but I gave it a try. Only issue :

  • openssl_random_pseudo_bytes(20) returns a string
  • bindec(openssl_random_pseudo_bytes(20)) returns 0
  • hexdec(bin2hex(bindec(openssl_random_pseudo_bytes(20))) returns 0
  • gmp_import is only available in php >=5.6.1

So, I gave up and tried to upgrade my php to php5.6. Based on this I did :

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php5.6

But if I run phpinfo(), the php version displayed is still 5.9.9.

Please, help ! I'm desperate. Thanks a lot !

Aucun commentaire:

Enregistrer un commentaire