Documentation
¶
Index ¶
- Constants
- Variables
- func Add(a, b interface{}) (string, error)
- func AddStr(input ...interface{}) string
- func AddStrLen(length int, input ...interface{}) string
- func AesCBCDecrypt(encryptedData string, key string) ([]byte, error)
- func AesCBCDecryptBase(encryptedData string, key []byte) ([]byte, error)
- func AesCBCEncrypt(plaintext []byte, key string) (string, error)
- func AesCBCEncryptBase(plaintext, key []byte) (string, error)
- func AesCBCEncryptWithIV(plaintext, key, iv []byte) (string, error)
- func AesGCMDecrypt(encryptedData string, key string) ([]byte, error)
- func AesGCMDecryptBase(encryptedData string, key, additionalData []byte) ([]byte, error)
- func AesGCMDecryptBaseByteResult(encryptedData, key, additionalData []byte) ([]byte, error)
- func AesGCMDecryptWithAAD(encryptedData string, key, additionalData string) ([]byte, error)
- func AesGCMEncrypt(plaintext []byte, key string) (string, error)
- func AesGCMEncryptBase(plaintext, key, additionalData []byte) (string, error)
- func AesGCMEncryptBaseByteResult(plaintext, key, additionalData []byte) ([]byte, error)
- func AesGCMEncryptWithAAD(plaintext []byte, key, additionalData string) (string, error)
- func AnyToStr(any interface{}) string
- func Base64Decode(input interface{}) []byte
- func Base64DecodeWithPool(input interface{}) []byte
- func Base64Encode(input interface{}) string
- func Base64EncodeWithPool(input interface{}) string
- func Bytes2Str(b []byte) string
- func CheckInt(c int, vs ...int) bool
- func CheckInt32(c int32, vs ...int32) bool
- func CheckInt64(c int64, vs ...int64) bool
- func CheckLen(o interface{}, min, max int) bool
- func CheckRangeInt(c, min, max int) bool
- func CheckRangeInt64(c, min, max int64) bool
- func CheckStr(c string, vs ...string) bool
- func CheckStrLen(str string, min, max int) bool
- func ClientIP(req *http.Request) string
- func Cmp(a, b interface{}) int
- func CompareBase64Sign(sign []byte, target string) bool
- func CompareSign(sign, target []byte) bool
- func Div(a, b interface{}, n int32) (string, error)
- func Error(input ...interface{}) error
- func FNV1a64(s string) string
- func FNV1a64Base(b []byte) string
- func Float64ToArgs(data []float64) []interface{}
- func Float64ToInt64(f float64) int64
- func FmtDiv(v string, d int64) string
- func FmtEL(msg string, values ...interface{}) (string, error)
- func FmtZero(r string) string
- func GetAesIVSecure() []byte
- func GetAesKeySecure(key string) []byte
- func GetAnyDayFirstAndLast(x int64) (int64, int64)
- func GetAnyMonthFirstAndLast(month int) (int64, int64)
- func GetDayFirstAndLast() (int64, int64)
- func GetFmtDate(t int64) int64
- func GetInDayFirstAndLast(x int64) (int64, int64)
- func GetJsonBool(b []byte, k string) bool
- func GetJsonBytes(b []byte, k string) []byte
- func GetJsonFloat64(b []byte, k string) float64
- func GetJsonInt(b []byte, k string) int
- func GetJsonInt64(b []byte, k string) int64
- func GetJsonObjectBytes(b []byte, k string) []byte
- func GetJsonObjectString(b []byte, k string) string
- func GetJsonObjectValue(b []byte) *fastjson.Value
- func GetJsonString(b []byte, k string) string
- func GetLocalDynamicSecretKey() string
- func GetLocalIP() string
- func GetMonthFirstAndLast() (int64, int64)
- func GetPath() string
- func GetRandomSecure(l int) []byte
- func GetSnowflakeNode(n int64) *snowflake.Node
- func GetUUID(noHyphens bool) string
- func GetWeekFirstAndLast() (int64, int64)
- func HMAC_MD5(data, key string, useBase64 ...bool) string
- func HMAC_MD5_BASE(data, key []byte) []byte
- func HMAC_SHA1(data, key string, useBase64 ...bool) string
- func HMAC_SHA1_BASE(data, key []byte) []byte
- func HMAC_SHA256(data, key string, useBase64 ...bool) string
- func HMAC_SHA256_BASE(data, key []byte) []byte
- func HMAC_SHA512(data, key string, useBase64 ...bool) string
- func HMAC_SHA512_BASE(data, key []byte) []byte
- func HasStr(s1 string, s2 string) bool
- func InArray(p interface{}) []interface{}
- func Int2Time(t int64) time.Time
- func Int64ToArgs(data []int64) []interface{}
- func IsAccount(s string) bool
- func IsEmail(s string) bool
- func IsFloat(s string) bool
- func IsIDNO(s string) bool
- func IsIPV4(s string) bool
- func IsInt(s string) bool
- func IsMobil(s string) bool
- func IsMoney(s string) bool
- func IsMoney2(s string) bool
- func IsMoney3(s string) bool
- func IsNumber(s string) bool
- func IsNumber2(s string) bool
- func IsPKNO(s interface{}) bool
- func IsPassword(s string) bool
- func IsURL(s string) bool
- func JsonMarshal(v interface{}) ([]byte, error)
- func JsonToAny(src interface{}, target interface{}) error
- func JsonUnmarshal(data []byte, v interface{}) error
- func JsonUnmarshalFast(data []byte, v interface{}) error
- func JsonUnmarshalUseNumber(data []byte, v interface{}) error
- func JsonUnmarshalUseNumberFast(data []byte, v interface{}) error
- func JsonValid(b []byte) bool
- func JsonValidString(s string) bool
- func Len(o interface{}) int
- func LinuxOS() bool
- func LoadYamlConfigFromPath(path string) (*DIC.YamlConfig, error)
- func LowerFirst(str string) string
- func MD5(s string, useBase64 ...bool) string
- func MD5_BASE(s []byte) []byte
- func MacOS() bool
- func MatchFilterURL(requestPath string, matchPattern []string) bool
- func MathAbs(n int64) int64
- func ModRand(n int) int
- func Mul(a, b interface{}) (string, error)
- func NewPasswordBase(pwd, salt []byte, iter int) []byte
- func NextIID() int64
- func NextIIDByNode(node int64) int64
- func NextSID() string
- func NextSIDByNode(node int64) string
- func PKCS7Padding(ciphertext []byte, blockSize int) []byte
- func PKCS7UnPadding(plantText []byte, blockSize int) []byte
- func ParseJsonBase64(input interface{}, ouput interface{}) error
- func RandInt(n int) string
- func RandNonce() string
- func RandProtocolNonce() string
- func RandStr(n int, b ...bool) string
- func ReadFile(path string) ([]byte, error)
- func ReadJsonConfig(conf []byte, result interface{}) error
- func ReadLocalJsonConfig(path string, result interface{}) error
- func ReadLocalYamlConfig(path string, result interface{}) error
- func Reverse(s string) string
- func ReverseBase64(s string) string
- func ReverseStr(s string, x ...int) string
- func SHA1(s string, useBase64 ...bool) string
- func SHA1_BASE(s []byte) []byte
- func SHA256(s string, useBase64 ...bool) string
- func SHA256_BASE(s []byte) []byte
- func SHA512(s string, useBase64 ...bool) string
- func SHA512_BASE(s []byte) []byte
- func SetDynamicSecretKey(key string)
- func Shift(input interface{}, ln int, fz bool) string
- func ShiftN(a interface{}, n int32) (string, error)
- func StartWait(msg string)
- func Str2Bytes(s string) []byte
- func Str2Date(s string) (int64, error)
- func Str2FormatTime(s string, fmt string, local *time.Location) (int64, error)
- func Str2Time(s string) (int64, error)
- func StrToBool(str string) (bool, error)
- func StrToFloat(str string) (float64, error)
- func StrToInt(str string) (int, error)
- func StrToInt8(str string) (int8, error)
- func StrToInt16(str string) (int16, error)
- func StrToInt32(str string) (int32, error)
- func StrToInt64(str string) (int64, error)
- func StringToArgs(data []string) []interface{}
- func Sub(a, b interface{}) (string, error)
- func Substr(str string, start int, end int) string
- func Time2DateStr(t int64) string
- func Time2FormatStr(t int64, fmt string, local *time.Location) string
- func Time2Str(t int64) string
- func ToJsonBase64(input interface{}) (string, error)
- func UnixMilli() int64
- func UnixNano() int64
- func UnixSecond() int64
- func UpperFirst(str string) string
- func ValidPattern(content, pattern string) bool
- func ValidProtocolNonce(nonce string) bool
- func ValidProtocolNonceBytes(n []byte) bool
- func WindowsOS() bool
- type Base64Pool
Constants ¶
const ( TimeFmt = "2006-01-02 15:04:05.000" TimeFmt2 = "2006-01-02 15:04:05.000000" DateFmt = "2006-01-02" OneDay = 86400000 OneWeek = OneDay * 7 TwoWeek = OneDay * 14 OneMonth = OneDay * 30 )
const ( MOBILE = "^1[3456789]\\d{9}$" // 手机号码 INTEGER = "^[\\-]?[1-9]+[0-9]*$|^[0]$" // 包含+-的自然数 FLOAT = "^[\\-]?[1-9]+[\\.][0-9]+$|^[\\-]?[0][\\.][0-9]+$" // 包含+-的浮点数 IPV4 = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$" // IPV4地址 EMAIL = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" // 邮箱地址 ACCOUNT = "^[a-zA-Z][a-zA-Z0-9_]{5,14}$" // 账号格式 PASSWORD = "^.{6,18}$" // 密码格式 URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?$" // URL格式 IDNO = "(^\\d{18}$)|(^\\d{15}$)" // 身份证格式 PKNO = "^1([3-9]{1})([0-9]{17})$" // 主键ID格式 NUMBER = "(^[1-9]([0-9]{0,29})$)|(^(0){1}$)" // 自然数 NUMBER2 = "^[0-9]+$" // 纯数字 MONEY = "(^[1-9]([0-9]{0,10})$)" // 自然数金额格式 MONEY2 = "(^[1-9]([0-9]{0,12})?(\\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\\.[0-9]([0-9])?$)" // 包含+-自然数金额格式 MONEY3 = "(^(-)?[1-9]([0-9]{0,12})?(\\.[0-9]{1,2})?$)|(^(0){1}$)|(^(-)?[0-9]\\.[0-9]([0-9])?$)" // 包含+-的浮点数金额格式 )
const ProtocolNonceBytes = 32
ProtocolNonceBytes HTTP/WebSocket/RPCX 协议随机数(n / Nonce)固定长度:32 字节。
Variables ¶
var (
CstSH, _ = time.LoadLocation("Asia/Shanghai") //上海
)
var (
SPEL = regexp.MustCompile(`\$\{([^}]+)\}`)
)
Functions ¶
func AesCBCDecrypt ¶
AesCBCDecrypt 字符串版本的便捷方法
func AesCBCDecryptBase ¶
AesCBCDecryptBase 使用指定IV的AES-CBC解密
func AesCBCEncrypt ¶
AesCBCEncrypt 字符串版本的便捷方法
func AesCBCEncryptBase ¶
AesCBCEncryptBase 标准AES-CBC加密,返回IV+密文的Base64编码
func AesCBCEncryptWithIV ¶
AesCBCEncryptWithIV 使用指定IV的AES-CBC加密
func AesGCMDecrypt ¶
AesGCMDecrypt AES-GCM 解密(带认证验证)
func AesGCMDecryptBase ¶
AesGCMDecryptBase AES-GCM 解密基础方法 会自动验证: 1. 认证标签(AuthTag)- 确保密文未被篡改 2. 附加认证数据(AAD)- 确保关联数据未被篡改 任何一项验证失败都会返回错误,拒绝解密
func AesGCMDecryptBaseByteResult ¶
AesGCMDecryptBaseByteResult AES-GCM 解密基础方法 会自动验证: 1. 认证标签(AuthTag)- 确保密文未被篡改 2. 附加认证数据(AAD)- 确保关联数据未被篡改 任何一项验证失败都会返回错误,拒绝解密
func AesGCMDecryptWithAAD ¶
AesGCMDecryptWithAAD AES-GCM 解密(带附加认证数据验证) additionalData: 必须与加密时使用的 AAD 完全一致
func AesGCMEncrypt ¶
AesGCMEncrypt AES-GCM 加密(带认证)- 推荐用于金融/银行级应用 相比 CBC 的优势: 1. 内置完整性保护(GMAC 认证标签) 2. 防止密文篡改攻击 3. 无需 PKCS7 Padding(支持任意长度) 4. 并行加密(性能提升 2-5 倍) 5. TLS 1.3 强制使用 6. NIST/PCI DSS 优先推荐
func AesGCMEncryptBase ¶
AesGCMEncryptBase AES-GCM 加密基础方法 返回格式:Base64(Nonce + Ciphertext + AuthTag) Nonce: 12 字节(GCM 标准) AuthTag: 16 字节(128-bit 认证标签)
func AesGCMEncryptBaseByteResult ¶
AesGCMEncryptBaseByteResult AES-GCM 加密基础方法 返回格式:Byte(Nonce + Ciphertext + AuthTag) Nonce: 12 字节(GCM 标准) AuthTag: 16 字节(128-bit 认证标签)
func AesGCMEncryptWithAAD ¶
AesGCMEncryptWithAAD AES-GCM 加密(带附加认证数据) additionalData: 不加密但需要认证的数据(如请求头、用户ID等) 使用场景:确保关联数据(如交易ID、用户ID)未被篡改
func Base64DecodeWithPool ¶
func Base64DecodeWithPool(input interface{}) []byte
Base64DecodeWithPool 使用缓冲池进行Base64解码的便捷函数
func Base64EncodeWithPool ¶
func Base64EncodeWithPool(input interface{}) string
Base64EncodeWithPool 使用缓冲池进行Base64编码的便捷函数
func Bytes2Str ¶
Bytes2Str 零拷贝转换 []byte 为 string
⚠️ 重要警告:返回的 string 与原始 []byte 共享内存
使用场景:
- SQL 驱动返回的 []byte(不会被修改)
- 网络协议解析的 []byte(不会被修改)
- 任何不会被修改的 []byte
性能:
- 0.10 ns/op(比 string(b) 快 200x)
- 0 B/op 零内存分配
注意:
- 不要修改原始 []byte,否则会破坏 string 的不可变性
- 此转换对任何 len/cap 组合都是正确的(总是读取 len 字段)
- 此实现依赖 Go 内部 slice/string 内存布局,未来 Go 版本可能失效
示例:
b := []byte("hello")
s := Bytes2Str(b)
fmt.Println(s) // ✅ 安全:只读访问
b[0] = 'H' // ⚠️ 危险!会修改 s 的内容
func CompareBase64Sign ¶
func CompareSign ¶
CompareSign 防止时序攻击, sign=自身有效签名 target=外部不可信签名
func Float64ToArgs ¶
func Float64ToArgs(data []float64) []interface{}
func GetJsonBool ¶
func GetJsonBytes ¶
func GetJsonFloat64 ¶
func GetJsonInt ¶
func GetJsonInt64 ¶
func GetJsonObjectBytes ¶
func GetJsonObjectString ¶
func GetJsonObjectValue ¶
GetJsonObjectValue 例如: v.Get("a").Get("b").MarshalTo(nil)
func GetJsonString ¶
func GetLocalDynamicSecretKey ¶
func GetLocalDynamicSecretKey() string
func GetRandomSecure ¶
GetRandomSecure 从 crypto/rand 读取 l 字节。失败时 panic:视为系统级故障, 继续用弱随机源(尤其作 AES-GCM nonce)会破坏安全假设。
func GetSnowflakeNode ¶
func HMAC_SHA1_BASE ¶
HMAC_SHA1_BASE 返回原始字节数组的HMAC-SHA1
func HMAC_SHA256_BASE ¶
HMAC_SHA256_BASE 返回原始字节数组的HMAC-SHA256
func HMAC_SHA512 ¶
func HMAC_SHA512_BASE ¶
func Int64ToArgs ¶
func Int64ToArgs(data []int64) []interface{}
func IsPassword ¶
func JsonUnmarshalFast ¶
JsonUnmarshalFast 单遍 JSON 反序列化(跳过 JsonValid 预校验)。 适用于高频热路径:直接反序列化,失败时由反序列化错误返回。
func JsonUnmarshalUseNumber ¶
JsonUnmarshalUseNumber 反序列化 JSON;数字保留为 json.Number,避免雪花 ID 经 float64 丢精度。 适用于 audit 脱敏等解析到 interface{} / map[string]interface{} 后再 Marshal 的场景。
func JsonUnmarshalUseNumberFast ¶
JsonUnmarshalUseNumberFast 单遍反序列化(跳过 JsonValid),UseNumber 保留大整数精度。
func LoadYamlConfigFromPath ¶
func LoadYamlConfigFromPath(path string) (*DIC.YamlConfig, error)
LoadYamlConfigFromPath 从指定路径读取配置文件
func MatchFilterURL ¶
func NewPasswordBase ¶
func NextIIDByNode ¶
NextIIDByNode 获取指定node的雪花int64 ID 适用于多进程/多服务并发写同库时通过区分node降低主键冲突风险。
func PKCS7Padding ¶
func PKCS7UnPadding ¶
func ParseJsonBase64 ¶
func ParseJsonBase64(input interface{}, ouput interface{}) error
func RandProtocolNonce ¶
func RandProtocolNonce() string
RandProtocolNonce 生成协议用 Nonce:Base64(32 字节 CSPRNG)。
func ReadLocalJsonConfig ¶
读取本地JSON配置文件
func ReadLocalYamlConfig ¶
读取本地YAML配置文件
func ReverseBase64 ¶
func ReverseStr ¶
func SetDynamicSecretKey ¶
func SetDynamicSecretKey(key string)
func Str2Bytes ¶
Str2Bytes 零拷贝转换 string 为 []byte
⚠️ 重要警告:返回的 []byte 与原始 string 共享内存
使用场景:
- 只读访问(哈希计算、HMAC、加密等)
- 临时传参(不会被修改的场景)
- 性能关键路径(高频调用)
性能:
- 0.10 ns/op(比 []byte(s) 快 200x)
- 0 B/op 零内存分配
注意:
- string 在 Go 中是不可变的(immutable),底层数据存储在只读内存段
- 尝试修改返回的 []byte 会触发运行时 panic(写入只读内存)
- 返回的 []byte 保证 len == cap
- 此实现依赖 Go 内部 string/slice 内存布局,未来版本可能失效
示例:
s := "hello" b := Str2Bytes(s) hash := md5.Sum(b) // ✅ 安全:只读访问 b[0] = 'H' // ⚠️ 运行时 panic:尝试写入只读内存
func Str2FormatTime ¶
格式字符串转时间戳/毫秒
func StringToArgs ¶
func StringToArgs(data []string) []interface{}
func Time2FormatStr ¶
时间戳转格式字符串/毫秒
func ToJsonBase64 ¶
func ValidPattern ¶
func ValidProtocolNonce ¶
ValidProtocolNonce 校验 Nonce 为 Base64 编码且解码后恰好 32 字节。
func ValidProtocolNonceBytes ¶
ValidProtocolNonceBytes 校验原始字节 Nonce(如 RPCX CommonRequest.n)。
Types ¶
type Base64Pool ¶
type Base64Pool struct {
// contains filtered or unexported fields
}
Base64Pool Base64编解码缓冲池
func (*Base64Pool) Decode ¶
func (p *Base64Pool) Decode(input interface{}) []byte
Decode 使用缓冲池进行Base64解码
func (*Base64Pool) Encode ¶
func (p *Base64Pool) Encode(input interface{}) string
Encode 使用缓冲池进行Base64编码