Hashids

作者: 星塵子 | 来源:发表于2020-02-28 13:20 被阅读0次

Hahsids 简介

  1. 简单的说就是将数字加密为简短的、唯一的、非顺序的ID的开源库。
  2. 主要用途就是将自增长的主键变为无法猜测的ID。
  3. 可自定义用于生成 ID 的字符及用于加密的salt,以及生成ID 的长度。

详见官网

demo


import (
    "fmt"
    "github.com/speps/go-hashids"
)
    
func main() {
    //定义规则
    hd := hashids.NewData()
    //id 可用字符
    hd.Alphabet = "ABCDEFGabcdefg1234567890"
    //盐
    hd.Salt = "this is my salt"
    //长度
    hd.MinLength = 10
    
    h, _ := hashids.NewWithData(hd)
    
    for i := 0; i < 5 ; i++ {
        e, _ := h.Encode([]int{ i })
        d, _ := h.DecodeWithError(e)
        fmt.Println(i, " encode:", e," len:", len(e), " decode: ", d[0])
    }
}
1.png

相关文章

网友评论

      本文标题:Hashids

      本文链接:https://www.haomeiwen.com/subject/umnkhhtx.html