2 Commits
v0.1.2 ... main

2 changed files with 10 additions and 10 deletions

View File

@@ -29,7 +29,7 @@ func generateKey(key string) []byte {
// 对给定的数据进行加密。
func Encrypt(data string, strength ...Strength) (string, error) {
var ivGen aes.IVGenerator
if append(strength, Compatible)[0] == Compatible {
if append(strength, Enhanced)[0] == Compatible {
ivGen = aes.PrefixIVGenerator
} else {
ivGen = aes.XorIVGenerator
@@ -50,7 +50,7 @@ func Encrypt(data string, strength ...Strength) (string, error) {
// 对给定的数据进行解密。
func Decrypt(data string, strength ...Strength) (string, error) {
var ivGen aes.IVGenerator
if append(strength, Compatible)[0] == Compatible {
if append(strength, Enhanced)[0] == Compatible {
ivGen = aes.PrefixIVGenerator
} else {
ivGen = aes.XorIVGenerator

View File

@@ -10,17 +10,17 @@ import (
)
// 用于记录当前冰雹ID组成内容的数据结构
type _HailAlgorithm struct {
type HailAlgorithm struct {
hostSerial int64
lastTimestamp int64
lastSequence int64
lock sync.Mutex
}
var hailAlgorithmInstance *_HailAlgorithm
var hailAlgorithmInstance *HailAlgorithm
// 获取当前已经完成初始化的冰雹ID生成算法实例如果尚未完成初始化则会返回未初始化的错误。
func Get() (*_HailAlgorithm, error) {
func Get() (*HailAlgorithm, error) {
if hailAlgorithmInstance == nil {
return nil, &HailAlgorithmNotInitializedError{}
}
@@ -29,7 +29,7 @@ func Get() (*_HailAlgorithm, error) {
// 指定一个主机编号并完成冰雹ID生成算法的初始化。
func Initialize(hostSerial int64) {
hailAlgorithmInstance = &_HailAlgorithm{
hailAlgorithmInstance = &HailAlgorithm{
hostSerial: hostSerial,
lastTimestamp: types.Timestamp(),
lastSequence: 0,
@@ -38,7 +38,7 @@ func Initialize(hostSerial int64) {
}
// 返回一个可用的时间戳,如果主机发生了时间回拨,那么将等待一秒钟。
func (h *_HailAlgorithm) timestamp() int64 {
func (h *HailAlgorithm) timestamp() int64 {
for {
timestamp := types.Timestamp()
if timestamp == h.lastTimestamp {
@@ -54,7 +54,7 @@ func (h *_HailAlgorithm) timestamp() int64 {
}
// 生成一个冰雹ID。
func (h *_HailAlgorithm) Generate() int64 {
func (h *HailAlgorithm) Generate() int64 {
h.lock.Lock()
defer h.lock.Unlock()
timestamp := h.timestamp()
@@ -63,11 +63,11 @@ func (h *_HailAlgorithm) Generate() int64 {
}
// 生成一个冰雹ID并将其转换为字符串。
func (h *_HailAlgorithm) GenerateString() string {
func (h *HailAlgorithm) GenerateString() string {
return fmt.Sprintf("%017d", h.Generate())
}
// 生成一个冰雹ID将其转换为字符串并附加指定的前缀
func (h *_HailAlgorithm) GeneratePrefixedString(prefix string) string {
func (h *HailAlgorithm) GeneratePrefixedString(prefix string) string {
return fmt.Sprintf("%s%s", prefix, h.GenerateString())
}