kkk212
V2EX  ›  PHP

根据 id 生成唯一短字符串(防止用户 id 等暴露), 用 hashids 稳妥不?

  •  
  •   kkk212 · Dec 19, 2018 · 4440 views
    This topic created in 2713 days ago, the information mentioned may be changed or developed.
    https://hashids.org/
    Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers.

    It converts numbers like 347 into strings like “ yr8 ”, or array of numbers like [27, 986] into “ 3kTMd ”.

    You can also decode those ids back. This is useful in bundling several parameters into one or simply using them as short UIDs.

    比如字符串长度设置 4-6 位,等用户量大了接近 4-6 位字符能表示的用户量时, 会不会出现重复的。
    2 replies    2018-12-21 09:31:12 +08:00
    jifengg
        1
    jifengg  
       Dec 19, 2018
    如果区分大小写,26 个字母+10 个数字,6 位可以有 (26+26+10)^6=56800235584,568 亿个不重复的字符串。
    但是位数少了有一个问题就是重复率会增加,需要排重的时间会增加。可以的情况下,位数尽可能多一些。
    trowa8509
        2
    trowa8509  
       Dec 21, 2018
    用 JWT 吧。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6185 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 355ms · UTC 02:57 · PVG 10:57 · LAX 19:57 · JFK 22:57
    ♥ Do have faith in what you're doing.