V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kkk212
V2EX  ›  PHP

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

  •  
  •   kkk212 · 2018-12-19 11:42:03 +08:00 · 3333 次点击
    这是一个创建于 1954 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2018-12-21 09:31:12 +08:00
    jifengg
        1
    jifengg  
       2018-12-19 14:40:52 +08:00
    如果区分大小写,26 个字母+10 个数字,6 位可以有 (26+26+10)^6=56800235584,568 亿个不重复的字符串。
    但是位数少了有一个问题就是重复率会增加,需要排重的时间会增加。可以的情况下,位数尽可能多一些。
    trowa8509
        2
    trowa8509  
       2018-12-21 09:31:12 +08:00
    用 JWT 吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3103 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:59 · PVG 22:59 · LAX 07:59 · JFK 10:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.