Compare commits
3 Commits
v0.1.2
...
66ccb82415
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66ccb82415 | ||
|
|
1dd98c25df | ||
|
|
68f052ef00 |
@@ -35,12 +35,16 @@ Golang 中可以使用的常用辅助功能工具箱。主要配备以下功能
|
|||||||
- [ ] BlockHash 散列算法
|
- [ ] BlockHash 散列算法
|
||||||
- 唯一序列号生成器
|
- 唯一序列号生成器
|
||||||
- [x] 冰雹 ID 生成器(短主机精简日期版雪花 ID)
|
- [x] 冰雹 ID 生成器(短主机精简日期版雪花 ID)
|
||||||
- [x] UUID 生成器
|
- [x] UUIDv4 生成器
|
||||||
|
- [ ] UUIDv7 生成器
|
||||||
|
- [ ] UUIDv7 比较及排序
|
||||||
|
- [ ] 基于 Base36 的 short UUIDv7 转换器
|
||||||
- [x] short UUID 生成器
|
- [x] short UUID 生成器
|
||||||
- 验证码生成器
|
- 验证码生成器
|
||||||
- [x] 随机验证码生成算法
|
- [x] 随机验证码生成算法
|
||||||
- 序列化算法
|
- 序列化算法
|
||||||
- [x] Base64 算法
|
- [x] Base64 算法
|
||||||
|
- [ ] Base36 算法
|
||||||
- [x] Hex 直转
|
- [x] Hex 直转
|
||||||
- 常用工具函数
|
- 常用工具函数
|
||||||
- [ ] 日期时间函数
|
- [ ] 日期时间函数
|
||||||
|
|||||||
@@ -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