Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1dd98c25df | ||
|
68f052ef00 |
@@ -29,7 +29,7 @@ func generateKey(key string) []byte {
|
|||||||
// 对给定的数据进行加密。
|
// 对给定的数据进行加密。
|
||||||
func Encrypt(data string, strength ...Strength) (string, error) {
|
func Encrypt(data string, strength ...Strength) (string, error) {
|
||||||
var ivGen aes.IVGenerator
|
var ivGen aes.IVGenerator
|
||||||
if append(strength, Compatible)[0] == Compatible {
|
if append(strength, Enhanced)[0] == Compatible {
|
||||||
ivGen = aes.PrefixIVGenerator
|
ivGen = aes.PrefixIVGenerator
|
||||||
} else {
|
} else {
|
||||||
ivGen = aes.XorIVGenerator
|
ivGen = aes.XorIVGenerator
|
||||||
@@ -50,7 +50,7 @@ func Encrypt(data string, strength ...Strength) (string, error) {
|
|||||||
// 对给定的数据进行解密。
|
// 对给定的数据进行解密。
|
||||||
func Decrypt(data string, strength ...Strength) (string, error) {
|
func Decrypt(data string, strength ...Strength) (string, error) {
|
||||||
var ivGen aes.IVGenerator
|
var ivGen aes.IVGenerator
|
||||||
if append(strength, Compatible)[0] == Compatible {
|
if append(strength, Enhanced)[0] == Compatible {
|
||||||
ivGen = aes.PrefixIVGenerator
|
ivGen = aes.PrefixIVGenerator
|
||||||
} else {
|
} else {
|
||||||
ivGen = aes.XorIVGenerator
|
ivGen = aes.XorIVGenerator
|
||||||
|
@@ -10,17 +10,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 用于记录当前冰雹ID组成内容的数据结构
|
// 用于记录当前冰雹ID组成内容的数据结构
|
||||||
type _HailAlgorithm struct {
|
type HailAlgorithm struct {
|
||||||
hostSerial int64
|
hostSerial int64
|
||||||
lastTimestamp int64
|
lastTimestamp int64
|
||||||
lastSequence int64
|
lastSequence int64
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var hailAlgorithmInstance *_HailAlgorithm
|
var hailAlgorithmInstance *HailAlgorithm
|
||||||
|
|
||||||
// 获取当前已经完成初始化的冰雹ID生成算法实例,如果尚未完成初始化则会返回未初始化的错误。
|
// 获取当前已经完成初始化的冰雹ID生成算法实例,如果尚未完成初始化则会返回未初始化的错误。
|
||||||
func Get() (*_HailAlgorithm, error) {
|
func Get() (*HailAlgorithm, error) {
|
||||||
if hailAlgorithmInstance == nil {
|
if hailAlgorithmInstance == nil {
|
||||||
return nil, &HailAlgorithmNotInitializedError{}
|
return nil, &HailAlgorithmNotInitializedError{}
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ func Get() (*_HailAlgorithm, error) {
|
|||||||
|
|
||||||
// 指定一个主机编号,并完成冰雹ID生成算法的初始化。
|
// 指定一个主机编号,并完成冰雹ID生成算法的初始化。
|
||||||
func Initialize(hostSerial int64) {
|
func Initialize(hostSerial int64) {
|
||||||
hailAlgorithmInstance = &_HailAlgorithm{
|
hailAlgorithmInstance = &HailAlgorithm{
|
||||||
hostSerial: hostSerial,
|
hostSerial: hostSerial,
|
||||||
lastTimestamp: types.Timestamp(),
|
lastTimestamp: types.Timestamp(),
|
||||||
lastSequence: 0,
|
lastSequence: 0,
|
||||||
@@ -38,7 +38,7 @@ func Initialize(hostSerial int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 返回一个可用的时间戳,如果主机发生了时间回拨,那么将等待一秒钟。
|
// 返回一个可用的时间戳,如果主机发生了时间回拨,那么将等待一秒钟。
|
||||||
func (h *_HailAlgorithm) timestamp() int64 {
|
func (h *HailAlgorithm) timestamp() int64 {
|
||||||
for {
|
for {
|
||||||
timestamp := types.Timestamp()
|
timestamp := types.Timestamp()
|
||||||
if timestamp == h.lastTimestamp {
|
if timestamp == h.lastTimestamp {
|
||||||
@@ -54,7 +54,7 @@ func (h *_HailAlgorithm) timestamp() int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 生成一个冰雹ID。
|
// 生成一个冰雹ID。
|
||||||
func (h *_HailAlgorithm) Generate() int64 {
|
func (h *HailAlgorithm) Generate() int64 {
|
||||||
h.lock.Lock()
|
h.lock.Lock()
|
||||||
defer h.lock.Unlock()
|
defer h.lock.Unlock()
|
||||||
timestamp := h.timestamp()
|
timestamp := h.timestamp()
|
||||||
@@ -63,11 +63,11 @@ func (h *_HailAlgorithm) Generate() int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 生成一个冰雹ID,并将其转换为字符串。
|
// 生成一个冰雹ID,并将其转换为字符串。
|
||||||
func (h *_HailAlgorithm) GenerateString() string {
|
func (h *HailAlgorithm) GenerateString() string {
|
||||||
return fmt.Sprintf("%017d", h.Generate())
|
return fmt.Sprintf("%017d", h.Generate())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成一个冰雹ID,将其转换为字符串并附加指定的前缀
|
// 生成一个冰雹ID,将其转换为字符串并附加指定的前缀
|
||||||
func (h *_HailAlgorithm) GeneratePrefixedString(prefix string) string {
|
func (h *HailAlgorithm) GeneratePrefixedString(prefix string) string {
|
||||||
return fmt.Sprintf("%s%s", prefix, h.GenerateString())
|
return fmt.Sprintf("%s%s", prefix, h.GenerateString())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user