Kaynağa Gözat

添加 '_posts/go-v7.md'

aaronwei 5 yıl önce
ebeveyn
işleme
9e5fde21db
1 değiştirilmiş dosya ile 45 ekleme ve 0 silme
  1. 45 0
      _posts/go-v7.md

+ 45 - 0
_posts/go-v7.md

@@ -0,0 +1,45 @@
+---
+title: golang ID混淆
+date: 2020-07-08 14:00:00
+tags: Golang
+---
+
+## golang ID混淆
+
+### 用命令获取第三方包
+
+```bash
+go get -u -v github.com/speps/go-hashids
+
+```
+
+### 实现如下
+
+```go 
+package hashids
+
+import (
+    "github.com/speps/go-hashids"
+)
+
+//salt 盐值
+const salt = "salt"
+
+//Encode 混淆
+func Encode(data int) string {
+    hd := hashids.NewData()
+    hd.Salt = salt
+    h, _ := hashids.NewWithData(hd)
+    e, _ := h.Encode([]int{data})
+    return e
+}
+
+//Decode 还原混淆
+func Decode(data string) int {
+    hd := hashids.NewData()
+    hd.Salt = salt
+    h, _ := hashids.NewWithData(hd)
+    e, _ := h.DecodeWithError(data)
+    return e[0]
+}
+```