doudundian9558 2016-05-23 20:13
浏览 68
已采纳

uniqid()与更多熵的复制有多容易?

This might be an off topic question but i hope someone can answer this question.

Per how many nanoseconds, mili seconds or seconds does uniqid() with more entropy run the risk of creating a duplicate?

With reference to link below, uniqid will collide if two id are created in one milisecond. What about with more entropy?

(My goal is to use a small indexable alphanumeric string as document id at creation that can be created fast with minimum processor power without db interference.)

Answers here dont seem to provide any exact number: How unique is uniqid?

  • 写回答

1条回答 默认 最新

  • dqwn64004 2016-07-24 00:31
    关注

    From the source code, more_entropy adds nine random decimal digits, so you can expect a collision after 37,000 or so calls. (For how a billion turned into 37,000, see the birthday attack.) That of course ignores the fact that these digits are not actually random but generated by an LCG, and the same LCG is probably used in other places in the code, so the actual chance of collision is probably higher (by how much exactly, I have no idea).

    Also worth noting that uniqid does not actually guarantee microsecond resolution as some PHP implementations (Windows, specifically) don't have access to a microsecond-precision clock.

    In short, if you need a unique ID for anything security-sensitive, or collisions are costly, avoid uniqid. Otherwise, using it with more_entropy is probably fine (although the common pattern is to use uniqid(mt_rand(), true) to add even more extra entropy).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题