php - 我创建了一个伪随机生成器,它对密码学安全吗?

标签 php security random cryptography

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center为指导。




9年前关闭。




我为加密目的创建了一个随机生成器,我想知道它是否足够安全。如果是,你当然可以自由使用它。提前感谢您阅读我的文字墙:D

功能说明
$this->pref_hash_algo($bits_of_entropy=null, $inclusive=false)函数获取默认散列方法(在我的情况下 sha256 )或者如果 $bits_of_entropy给定它获得包含或不包含的最佳散列算法。例如,包含 230 位熵将返回 sha256而独家会给sha224 .

self::$HASH_PREFERENCES['128'][0] 返回的算法是 ripemd128
对于 $this->hash($input, $algorithm=null, $output_type=self::OUTPUT_HEX)它只做一个 PHP hash($input, $algorithm[, $binary])额外检查算法是否被认为是安全的,并支持比二进制和十六进制更多的输出类型。

代号

为方便访问,将第一个版本移至 http://pastebin.com/YtJFvpah

更新

根据您的输入,我已将代码更改为以下内容:http://pastebin.com/bQ5tFDdh

编辑摘要:

  • 不是散列,而只是格式化/dev/urandom 输出。
  • 添加了同时输出散列,用于当散列算法对于请求的随机位数量输出太少时(例如,当请求 4000 位时为 sha512)

  • 测试

    情况1

    我跑了php /my/path/to/file.php | ent测试 /dev/urandom方法和 2.000.000 字节样本的替代方法。
  • /dev/urandom给出每字节 7.999903 位的熵。 ( http://pastebin.com/NLqZ5Kza )
  • 另一种方法给出了每字节 7.99913 位的熵。 ( http://pastebin.com/rnebrvKg )

  • 案例二

    我使用替代方法创建了一个 4,7MB 的二进制文件(当 /dev/urandom 被禁用/不可用时)并运行 dieharder -a -f /home/beanow/random.input -t 10 :
  • /dev/urandom给出: 2 个弱点 ( http://pastebin.com/HiwQeJtP )
  • 替代方法给出: 0 弱点 ( http://pastebin.com/x1VbEhzg )

  • 案例3

    与案例 2 相同,但使用 20MB 二进制文件并删除 -t 参数以使用默认设置。
  • /dev/urandom给出: 4 个弱点 ( http://pastebin.com/hwqQBTqu )
  • 替代方法给出: 1 弱 ( http://pastebin.com/cvPXiGBV )
  • 最佳答案

    您可以针对您的输出运行许多测试,以确定它的真正随机性。

    ent - A Pseudorandom Number Sequence Test Program

    ... [ent] applies various tests to sequences of bytes stored in files and reports the results of those tests. [ent] is useful for evaluating pseudorandom number generators for encryption and statistical sampling applications, compression algorithms, and other applications where the information density of a file is of interest.



    The diehard test suite

    ... a battery of statistical tests for measuring the quality of a random number generator. Wikipedia



    The dieharder test suite

    The primary point of dieharder (like diehard before it) is to make it easy to time and test (pseudo)random number generators, both software and hardware, for a variety of purposes in research and cryptography.



    使用自制的伪随机数生成器,通过顽固和顽固的测试套件是相当困难的。

    关于php - 我创建了一个伪随机生成器,它对密码学安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9037327/

    相关文章:

    php - 必须使用事件访问 token 来查询 Laravel 中当前用户的信息

    python - 在数据库中存储用户和密码

    c# - 每次 Random.Next() 调用时返回所有数字的概率是否相同?

    arrays - 从数组中选择一个项目并将项目名称与另一个项目名称进行比较

    haskell - 具有MaybeT和RandT的Monad变压器堆栈

    php - 查询多个表输出重复的结果

    php - 与 Doctrine 得到相同的结果

    php - 隐藏已弃用的错误消息不起作用

    security - 如果跨组织启用 Chrome 开发工具,是否存在潜在的安全问题?

    security - SSL 中如何使用信任库和 keystore ?