V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
gaozihang
V2EX  ›  PHP

一个 PHP 扩展: 根据数字生成唯一的字符串 ID

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

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

    一个可以生成类似于 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 的功能,可以试用下这个扩展 😆~

    9 replies    2018-05-29 22:45:44 +08:00
    update
        1
    update  
       Apr 12, 2018
    什么原理
    yimaneilicj
        2
    yimaneilicj  
       Apr 12, 2018
    mark
    szopen
        3
    szopen  
       Apr 13, 2018
    对称加密数字,和没加密区别不大吧
    jourdon
        4
    jourdon  
       Apr 19, 2018
    uuid 嘛
    xjroot
        5
    xjroot  
       May 1, 2018 via iPhone
    这个看得很凶呢😊,可以可以,收藏了先。
    xjroot
        6
    xjroot  
       May 1, 2018 via iPhone
    @jourdon 感觉没那么简单吧😄,
    fhefh
        7
    fhefh  
       May 3, 2018
    @gaozihang 楼主 纯 php 代码实现哪儿有 准备学习下 能否给个链接地址
    Foolt
        8
    Foolt  
       May 29, 2018 via Android
    @fhefh
    @update

    目测就是把只有数字的 10 进制转为有数字有大小写字母的 36 进制或者是有数字有大小写字母有几个符号的若干进制
    Foolt
        9
    Foolt  
       May 29, 2018 via Android
    @szopen 把字母打乱,只要别泄露原始数字 ID 那一般人是破解不了的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4037 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
    ♥ Do have faith in what you're doing.