bit4sat/storage/shortid.go

55 lines
871 B
Go
Raw Permalink Normal View History

package storage
import (
2019-04-05 16:18:44 +00:00
"time"
2019-04-05 16:18:44 +00:00
"git.sp4ke.com/sp4ke/bit4sat/config"
"github.com/speps/go-hashids"
)
2019-04-05 16:18:44 +00:00
var ShortHD *hashids.HashIDData
var LongHD *hashids.HashIDData
2019-04-05 16:18:44 +00:00
func GetShortId() (string, error) {
hd, err := hashids.NewWithData(ShortHD)
if err != nil {
return "", err
}
now := int64(time.Now().UnixNano())
short, err := hd.EncodeInt64([]int64{now})
if err != nil {
return "", err
}
return short, nil
}
2019-04-05 16:18:44 +00:00
func GetLongId() (string, error) {
hd, err := hashids.NewWithData(LongHD)
if err != nil {
2019-04-05 16:18:44 +00:00
return "", err
}
2019-04-05 16:18:44 +00:00
now := int64(time.Now().UnixNano())
long, err := hd.EncodeInt64([]int64{now})
if err != nil {
return "", err
}
return long, nil
}
func init() {
ShortHD = hashids.NewData()
ShortHD.Salt = config.ShortIdSalt
ShortHD.MinLength = 8
LongHD = hashids.NewData()
LongHD.Salt = config.ShortIdSalt
LongHD.MinLength = 32
}