elcodedocle/cryptosecureprng

具有类似于mt_rand方法的接口的密码学安全的伪随机数生成器类

0.2.2 2024-09-02 16:19 UTC

This package is not auto-updated.

Last update: 2024-09-26 15:20:52 UTC


README

为稍有疑虑的公民提供的mt_rand

版权 (C) 2014 Gael Abadin
许可证: MIT Expat / 啤酒许可证

动机

你是否曾经想要一个加密安全的mt_rand()?我想对许多事情来说可能并不有用(也许完全无用),但最近我想创建一个,可以从字典中安全地随机选择单词,这样可以用作密码(是的,马。那是电池的夹具)。我可能做得有点过了...结果是我在实现一个具有类似mt_rand接口的“加密安全”PRNG上的尝试,包括从可用扩展和/dev/urandom中选择和使用最佳随机字节生成器的包装器(顺便说一下,它比单核上的mt_rand慢约100倍,所以如果你想分叉它,显然还有很大的改进空间 ;-))

如何使用

与mt_rand()类似,可以从给定范围内随机选择整数,遵循均匀分布

require_once 'CryptoSecurePRNG.php';
$secGen =  new synapp\info\tools\passwordgenerator\cryptosecureprng\CryptoSecurePRNG();
$randInt = $secGen->rand(); //between 0 and mt_getrandmax()
$randInt = $secGen->rand(1,100); //between 1 and 100
$randInt = $secGen->rand(-50,50); //between -50 and 50

您还可以获取一个随机字节的字符串

require_once 'CryptoSecurePRNG.php';
$secGen =  new synapp\info\tools\passwordgenerator\cryptosecureprng\CryptoSecurePRNG();
$stringLength = 20; // number of random chars to be generated
$stringOfRandomChars = $secGen->getRandomBytesString($stringLength); // generate a string of $stringLength random ascii chars (non printable too)

以下是使用matlab可视化输出的代码

// PHP code, uses cryptosecureprng rand() to generate the samples
require_once 'CryptoSecurePRNG.php';
$prng = new synapp\info\tools\passwordgenerator\cryptosecureprng\CryptoSecurePRNG();
$out=''; 
for ($i=0;$i<1280;$i++) for ($j=0;$j<720;$j++) { 
  $out .= $prng->rand(0,255).','.$prng->rand(0,255).','.$prng->rand(0,255).',';   
}
$fh = fopen('testout.txt','w');
fwrite ($fh, $out);
fclose($fh);
% Matlab code, reads and displays the generated samples
x=csvread('testout.txt');
C = reshape (x,720,1280,3);
C = uint8(C);
imwrite(C,'rgb_output.bmp');
hist(x,256);
saveas(gcf,'hist_output','png');

如果您想了解有关调整和可用参数的更多信息,请检查代码(或使用phpdocumentor生成文档)。

如果您喜欢这个类,请随意为我买一杯啤酒 ;-)

比特币:15i9QKZqLuNdcyseHpjpZiPcty6FMazxk2

狗狗币:DCjimHzRu25smyjnEb7V9qFuVyf6P2JjBf

paypal: http://goo.gl/iQd1UL

祝您玩得开心。-