sever_tb_LinkedMall-2026082.../server_tb_LinkedMall/pkg/crypto/crypto.go

25 lines
487 B
Go

package crypto
import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
)
// GenerateNonce 生成随机字符串,用于防重放
func GenerateNonce() string {
buf := make([]byte, 16)
_, _ = rand.Read(buf)
return hex.EncodeToString(buf)
}
// GenerateTimestamp 生成当前时间戳(毫秒)
func GenerateTimestamp() int64 {
return time.Now().UnixMilli()
}
// FormatTimestamp 格式化时间戳为字符串
func FormatTimestamp(ts int64) string {
return fmt.Sprintf("%d", ts)
}