V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
gaozihang
V2EX  ›  程序员

一个 PHP 扩展: 根据数字生成唯一的字符串 ID (欢迎 star, 欢迎共同讨论代码 😆)

  •  
  •   gaozihang · Apr 13, 2018 · 2179 views
    This topic created in 2938 days ago, the information mentioned may be changed or developed.

    github: https://github.com/cdoco/hashids.phpc

    一个可以生成类似于 YouTuBe 那种唯一字符串 ID 的功能,可以对用户隐藏数据库中的真正数字 ID

    原来有纯 php 代码实现的一个功能,现在把它封装成了一个 php 扩展,性能比纯 php 的版本提升了百倍左右

    performance comparison

    使用也比较简单:

    $hashids = new Hashids();
    
    $hash = $hashids->encode(1, 2, 3, 4, 5); // ADf9h9i0sQ
    $numbers = $hashids->decode($hash); // [1, 2, 3, 4, 5]
    
    //你还可以直接使用静态方法
    $hash = Hashids::encode(1, 2, 3, 4, 5); // ADf9h9i0sQ
    $numbers = Hashids::decode($hash); // [1, 2, 3, 4, 5]
    

    如果你项目中需要这种加密数字 id 的功能,可以试用下这个扩展 😆~

    HowToMakeLove
        1
    HowToMakeLove  
       Apr 13, 2018
    有机会试试
    xxxy
        2
    xxxy  
       Apr 13, 2018
    请教一下,楼主你的方法跟直接 hash 数字加盐有什么区别?
    gaozihang
        3
    gaozihang  
    OP
       Apr 13, 2018
    @xxxy hash 只能 encode,不能 decode 啊,这个可以把你加密的数字再解密出来。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3241 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 387ms · UTC 11:58 · PVG 19:58 · LAX 04:58 · JFK 07:58
    ♥ Do have faith in what you're doing.