EncryptID
This commit is contained in:
parent
eb577d2b1b
commit
8ff96d7255
7
go.mod
7
go.mod
|
@ -4,7 +4,12 @@ go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/sirupsen/logrus v1.9.0
|
github.com/sirupsen/logrus v1.9.0
|
||||||
|
github.com/speps/go-hashids v2.0.0+incompatible
|
||||||
gorm.io/gorm v1.25.5
|
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
|
||||||
|
)
|
||||||
|
|
17
number.go
17
number.go
|
@ -2,6 +2,7 @@ package goutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
"github.com/speps/go-hashids"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
@ -68,3 +69,19 @@ func Hash32(s string) uint32 {
|
||||||
}
|
}
|
||||||
return digest
|
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])
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue