You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
615 B
34 lines
615 B
package hashid
|
|
|
|
import "github.com/HFO4/cloudreve/pkg/conf"
|
|
import "github.com/speps/go-hashids"
|
|
|
|
// ID类型
|
|
const (
|
|
ShareID = iota // 分享
|
|
UserID // 用户
|
|
)
|
|
|
|
// HashEncode 对给定数据计算HashID
|
|
func HashEncode(v []int) (string, error) {
|
|
hd := hashids.NewData()
|
|
hd.Salt = conf.SystemConfig.HashIDSalt
|
|
|
|
h, err := hashids.NewWithData(hd)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
id, err := h.Encode(v)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// HashID 计算数据库内主键对应的HashID
|
|
func HashID(id uint, t int) string {
|
|
v, _ := HashEncode([]int{int(id), t})
|
|
return v
|
|
}
|