From 8ff96d7255877487fafb70a4df4fc746b7024053 Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Wed, 5 Jun 2024 18:51:23 +0800 Subject: [PATCH] EncryptID --- go.mod | 7 ++++++- number.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a0fbcf3..d479d2e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,12 @@ go 1.18 require ( github.com/sirupsen/logrus v1.9.0 + github.com/speps/go-hashids v2.0.0+incompatible gorm.io/gorm v1.25.5 ) -require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +require ( + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/number.go b/number.go index d7d0554..1df40aa 100644 --- a/number.go +++ b/number.go @@ -2,6 +2,7 @@ package goutil import ( "crypto/md5" + "github.com/speps/go-hashids" "sync" "sync/atomic" "time" @@ -68,3 +69,19 @@ func Hash32(s string) uint32 { } return digest } + +func EncryptID(data int64, salt string) string { + hd := hashids.NewData() + hd.Salt = salt + h, _ := hashids.NewWithData(hd) + e, _ := h.Encode([]int{int(data)}) + return e +} + +func DecryptID(data, salt string) int64 { + hd := hashids.NewData() + hd.Salt = salt + h, _ := hashids.NewWithData(hd) + e, _ := h.DecodeWithError(data) + return int64(e[0]) +}