EncryptID

This commit is contained in:
jiangyong27 2024-06-05 18:51:23 +08:00
parent eb577d2b1b
commit 8ff96d7255
2 changed files with 23 additions and 1 deletions

7
go.mod
View File

@ -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
)

View File

@ -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])
}