Hashids

2020-02-28  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读