Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1dd98c25df | ||
|
68f052ef00 | ||
|
811e056bb8 |
@@ -13,6 +13,13 @@ import (
|
|||||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Strength int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Compatible Strength = iota
|
||||||
|
Enhanced
|
||||||
|
)
|
||||||
|
|
||||||
// 根据给定的密钥字符串生成加解密使用的密钥。
|
// 根据给定的密钥字符串生成加解密使用的密钥。
|
||||||
func generateKey(key string) []byte {
|
func generateKey(key string) []byte {
|
||||||
keyBytes := sha512.Sha512([]byte(key))
|
keyBytes := sha512.Sha512([]byte(key))
|
||||||
@@ -20,10 +27,16 @@ func generateKey(key string) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 对给定的数据进行加密。
|
// 对给定的数据进行加密。
|
||||||
func Encrypt(data string) (string, error) {
|
func Encrypt(data string, strength ...Strength) (string, error) {
|
||||||
|
var ivGen aes.IVGenerator
|
||||||
|
if append(strength, Enhanced)[0] == Compatible {
|
||||||
|
ivGen = aes.PrefixIVGenerator
|
||||||
|
} else {
|
||||||
|
ivGen = aes.XorIVGenerator
|
||||||
|
}
|
||||||
key := verifyCode.RandStr(20)
|
key := verifyCode.RandStr(20)
|
||||||
keyBytes := generateKey(key)
|
keyBytes := generateKey(key)
|
||||||
cipherData, err := aes.Encrypt([]byte(data), keyBytes, encryption.PKCS7Padding)
|
cipherData, err := aes.Encrypt([]byte(data), keyBytes, encryption.PKCS7Padding, ivGen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("加密计算失败,%w", err)
|
return "", fmt.Errorf("加密计算失败,%w", err)
|
||||||
}
|
}
|
||||||
@@ -35,7 +48,13 @@ func Encrypt(data string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 对给定的数据进行解密。
|
// 对给定的数据进行解密。
|
||||||
func Decrypt(data string) (string, error) {
|
func Decrypt(data string, strength ...Strength) (string, error) {
|
||||||
|
var ivGen aes.IVGenerator
|
||||||
|
if append(strength, Enhanced)[0] == Compatible {
|
||||||
|
ivGen = aes.PrefixIVGenerator
|
||||||
|
} else {
|
||||||
|
ivGen = aes.XorIVGenerator
|
||||||
|
}
|
||||||
if message, found := strings.CutPrefix(data, "["); found {
|
if message, found := strings.CutPrefix(data, "["); found {
|
||||||
if len(message) > 20 {
|
if len(message) > 20 {
|
||||||
keySeed := message[:20]
|
keySeed := message[:20]
|
||||||
@@ -44,7 +63,7 @@ func Decrypt(data string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("密文损坏无法解析,%w", err)
|
return "", fmt.Errorf("密文损坏无法解析,%w", err)
|
||||||
}
|
}
|
||||||
plainText, err := aes.Decrypt(cipherData, key, encryption.PKCS7Padding)
|
plainText, err := aes.Decrypt(cipherData, key, encryption.PKCS7Padding, ivGen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("密文解密计算失败,%w", err)
|
return "", fmt.Errorf("密文解密计算失败,%w", err)
|
||||||
}
|
}
|
||||||
|
@@ -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