utils

package module
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2021 License: MIT Imports: 34 Imported by: 26

README

Go-Utils

常用的助手函数

若有直接引用的, 会在函数定义或目录 README 中注明来源, 保留 LICENSE, 感谢之!

安装

go get github.com/fufuok/utils

目录

package utils // import "github.com/fufuok/utils"

func AddString(s ...string) string
func AddStringBytes(s ...string) []byte
func AesCBCDeB58(s, key string) []byte
func AesCBCDeB64(s, key string) []byte
func AesCBCDeHex(s, key string) []byte
func AesCBCDePKCS7B58(s, key string) []byte
func AesCBCDePKCS7B64(s, key string) []byte
func AesCBCDePKCS7Hex(s, key string) []byte
func AesCBCDePKCS7StringB58(s, key string) string
func AesCBCDePKCS7StringB64(s, key string) string
func AesCBCDePKCS7StringHex(s, key string) string
func AesCBCDeStringB58(s, key string) string
func AesCBCDeStringB64(s, key string) string
func AesCBCDeStringHex(s, key string) string
func AesCBCDecrypt(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) (plaintext []byte)
func AesCBCDecryptE(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) ([]byte, error)
func AesCBCEnB58(b, key []byte) string
func AesCBCEnB64(b, key []byte) string
func AesCBCEnHex(b, key []byte) string
func AesCBCEnPKCS7B58(b, key []byte) string
func AesCBCEnPKCS7B64(b, key []byte) string
func AesCBCEnPKCS7Hex(b, key []byte) string
func AesCBCEnPKCS7StringB58(s, key string) string
func AesCBCEnPKCS7StringB64(s, key string) string
func AesCBCEnPKCS7StringHex(s, key string) string
func AesCBCEnStringB58(s, key string) string
func AesCBCEnStringB64(s, key string) string
func AesCBCEnStringHex(s, key string) string
func AesCBCEncrypt(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) (ciphertext []byte)
func AesCBCEncryptE(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) ([]byte, error)
func AesGCMDeB58(s, key, nonce string) []byte
func AesGCMDeB64(s, key, nonce string) []byte
func AesGCMDeHex(s, key, nonce string) []byte
func AesGCMDeStringB58(s, key, nonce string) string
func AesGCMDeStringB64(s, key, nonce string) string
func AesGCMDeStringHex(s, key, nonce string) string
func AesGCMDecrypt(ciphertext, key, nonce []byte) (plaintext []byte)
func AesGCMDecryptWithNonce(ciphertext, key, nonce, additionalData []byte) ([]byte, error)
func AesGCMEnB58(b, key []byte) (string, string)
func AesGCMEnB64(b, key []byte) (string, string)
func AesGCMEnHex(b, key []byte) (string, string)
func AesGCMEnStringB58(s, key string) (string, string)
func AesGCMEnStringB64(s, key string) (string, string)
func AesGCMEnStringHex(s, key string) (string, string)
func AesGCMEncrypt(plaintext, key []byte) (ciphertext []byte, nonce []byte)
func AesGCMEncryptWithNonce(plaintext, key, nonce, additionalData []byte) ([]byte, []byte, error)
func AssertEqual(t testing.TB, expected, actual interface{}, description ...string)
func AssertPanics(t *testing.T, title string, f func())
func B2S(b []byte) string
func B64Decode(s string) []byte
func B64Encode(b []byte) string
func B64UrlDecode(s string) []byte
func B64UrlEncode(b []byte) string
func BigComma(b *big.Int) string
func BigCommaf(v *big.Float) string
func Bigoom(n, b *big.Int) (float64, int)
func CallPath() string
func Comma(v int64) string
func Commaf(v float64) string
func Commai(v int) string
func Commau(v uint64) string
func CopyB2S(b []byte) string
func CopyBytes(b []byte) []byte
func CopyS2B(s string) []byte
func CopyString(s string) string
func Decrypt(value, secret string) string
func DesCBCDeB58(s, key string) []byte
func DesCBCDeB64(s, key string) []byte
func DesCBCDeHex(s, key string) []byte
func DesCBCDePKCS7B58(s, key string) []byte
func DesCBCDePKCS7B64(s, key string) []byte
func DesCBCDePKCS7Hex(s, key string) []byte
func DesCBCDePKCS7StringB58(s, key string) string
func DesCBCDePKCS7StringB64(s, key string) string
func DesCBCDePKCS7StringHex(s, key string) string
func DesCBCDeStringB58(s, key string) string
func DesCBCDeStringB64(s, key string) string
func DesCBCDeStringHex(s, key string) string
func DesCBCDecrypt(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) (plaintext []byte)
func DesCBCDecryptE(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) ([]byte, error)
func DesCBCEnB58(b, key []byte) string
func DesCBCEnB64(b, key []byte) string
func DesCBCEnHex(b, key []byte) string
func DesCBCEnPKCS7B58(b, key []byte) string
func DesCBCEnPKCS7B64(b, key []byte) string
func DesCBCEnPKCS7Hex(b, key []byte) string
func DesCBCEnPKCS7StringB58(s, key string) string
func DesCBCEnPKCS7StringB64(s, key string) string
func DesCBCEnPKCS7StringHex(s, key string) string
func DesCBCEnStringB58(s, key string) string
func DesCBCEnStringB64(s, key string) string
func DesCBCEnStringHex(s, key string) string
func DesCBCEncrypt(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) (ciphertext []byte)
func DesCBCEncryptE(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) ([]byte, error)
func EncodeUUID(id []byte) []byte
func Encrypt(value, secret string) string
func Executable(evalSymlinks ...bool) string
func ExecutableDir(evalSymlinks ...bool) string
func Get0Hour(t time.Time) time.Time
func Get0LastMonth(t time.Time) time.Time
func Get0LastWeek(t time.Time) time.Time
func Get0Minute(t time.Time) time.Time
func Get0Month(t time.Time) time.Time
func Get0NextMonth(t time.Time) time.Time
func Get0NextWeek(t time.Time) time.Time
func Get0Second(t time.Time) time.Time
func Get0Tomorrow(t time.Time) time.Time
func Get0Week(t time.Time) time.Time
func Get0Yesterday(t time.Time) time.Time
func GetBytes(v interface{}, defaultVal ...[]byte) []byte
func GetInt(v interface{}, defaultInt ...int) int
func GetMonthDays(t time.Time) int
func GetNotInternalIPv4(ip, defaultIP net.IP, flag ...bool) net.IP
func GetNotInternalIPv4String(ip, defaultIP string, flag ...bool) string
func GetSafeB2S(b []byte, defaultVal ...string) string
func GetSafeBytes(b []byte, defaultVal ...[]byte) []byte
func GetSafeS2B(s string, defaultVal ...[]byte) []byte
func GetSafeString(s string, defaultVal ...string) string
func GetString(v interface{}, defaultVal ...string) string
func GetenvDecrypt(key string, secret string) string
func Hash(b []byte, h hash.Hash) []byte
func Hmac(b []byte, key []byte, h func() hash.Hash) []byte
func HmacSHA1(b, key []byte) []byte
func HmacSHA1Hex(s, key string) string
func HmacSHA256(b, key []byte) []byte
func HmacSHA256Hex(s, key string) string
func HmacSHA512(b, key []byte) []byte
func HmacSHA512Hex(s, key string) string
func HumanBaseBytes(v uint64, base float64, sizes []string) string
func HumanBytes(v uint64) string
func HumanIBytes(v uint64) string
func HumanIntBytes(v int) string
func HumanIntIBytes(v int) string
func IPv42Long(ip net.IP) int
func IPv4String2Long(ip string) int
func InInts(slice []int, n int) bool
func InStrings(ss []string, s string) bool
func IsDir(s string) bool
func IsExist(s string) bool
func IsFile(s string) bool
func IsInternalIPv4(ip net.IP) bool
func IsInternalIPv4String(ip string) bool
func JoinBytes(b ...[]byte) []byte
func Logn(n, b float64) float64
func Long2IPv4(n int) net.IP
func Long2IPv4String(n int) string
func MD5(b []byte) []byte
func MD5Hex(s string) string
func MD5Reader(r io.Reader) (string, error)
func MD5Sum(filename string) (string, error)
func MaxInt(a, b int) int
func MinInt(a, b int) int
func MustBool(v interface{}) bool
func MustInt(v interface{}) int
func MustJSON(v interface{}) []byte
func MustJSONString(v interface{}) string
func MustString(v interface{}) string
func Padding(b []byte, bSize int, pkcs7 bool) []byte
func ParseHumanBytes(s string) (uint64, error)
func RandBytes(n int) []byte
func RandHex(nHalf int) string
func RandInt(min int, max int) int
func RandString(n int) string
func Round(v float64, precision int) float64
func RunPath() string
func S2B(s string) []byte
func SearchInt(slice []int, n int) int
func SearchString(ss []string, s string) int
func SetenvEncrypt(key, value, secret string) (string, error)
func Sha1(b []byte) []byte
func Sha1Hex(s string) string
func Sha256(b []byte) []byte
func Sha256Hex(s string) string
func Sha512(b []byte) []byte
func Sha512Hex(s string) string
func Str2Bytes(s string) (b []byte)
func StrToBytes(s string) []byte
func String2Bytes(s string) (b []byte)
func StringToBytes(s string) (b []byte)
func UUID() []byte
func UUIDShort() string
func UUIDSimple() string
func UUIDString() string
func UnPadding(b []byte, pkcs7 bool) []byte
func WaitNextMinute()
func WeightedChoiceIndex(choices []TChoice) int
func WeightedChoiceMap(choices map[interface{}]int) interface{}
func WeightedChoiceWeightsIndex(weights []int) int
type TChoice struct{ ... }
    func WeightedChoice(choices ...TChoice) TChoice
加解密小工具

见: envtools

获取内外网 IP 小工具

见: myip

或: https://github.com/fufuok/myip

package myip // import "github.com/fufuok/utils/myip"

func ExternalIP(v ...string) string
func ExternalIPAny(retries ...int) string
func ExternalIPv4() string
func ExternalIPv6() string
func InternalIP(dstAddr, network string) string
func InternalIPv4() string
func InternalIPv6() string
func LocalIP() string
func LocalIPv4s() (ips []string)
编码解码 base58

见: base58

或: https://github.com/fufuok/basex

package base58 // import "github.com/fufuok/utils/base58"

func CheckDecode(input string) (result []byte, version byte, err error)
func CheckEncode(input []byte, version byte) string
func Decode(b string) []byte
func Encode(b []byte) string
可排序全局唯一ID生成器

比 UUID 更快, 更短

见: xid

或: http://github.com/fufuok/xid

package xid // import "github.com/fufuok/utils/xid"

func NewBytes() []byte
func NewString() string
func Sort(ids []ID)
type ID [rawLen]byte
    func FromBytes(b []byte) (ID, error)
    func FromString(id string) (ID, error)
    func New() ID
    func NewWithTime(t time.Time) ID
    func NilID() ID

JSON

json 使用 gin 类似的可选组织方式:

  • go build . 默认使用 json-iterator/go
  • go build -tags=gojson. 使用标准 JSON 库 encoding/json
  • go build -tags=go_json . 使用 goccy/go-json (暂不成熟, 观望中)

使用

var s string
s = utils.GetString(123.45)         // "123.45"
s = utils.GetString(nil)            // ""
s = utils.GetString(nil, "default") // "default"
s = utils.GetString([]byte("ff"))   // "ff"
s = utils.GetString(true)           // "true"

x := utils.AddString(s, "OK") // "trueOK"

b = []byte("trueOK")
s = utils.B2S(b[0:1])                                     // "t"
safeS1 := utils.B2S([]byte(s[0:1]))                       // 转换为不可变字符串, CopyString 的实现
safeS2 := utils.CopyString(s[0:1])                        // 不可变字符串, s 可以被 GC 回收
safeS3 := utils.GetSafeString(s[0:1], "optional default") // 不可变字符串
safeS4 := utils.GetSafeB2S(b[0:1], "optional default")    // 转换为不可变字符串
safeS5 := string(b[0:1])                                  // 标准转换
b[0] = 70                                                 // 注意: 底层数组变化会引起字符串 s 发生改变
fmt.Println(s, safeS1, safeS2, safeS3, safeS4, safeS5)    // F t t t t t

x = utils.AesCBCEnPKCS7StringHex("myData", "myKey")
fmt.Println(x)

x = utils.UUIDString()
fmt.Println(x)  // 04a49f17-8c37-44f7-a9c5-ea291c3736d7
x = utils.UUIDSimple()
fmt.Println(x)  // 16123e98b35a4cea8e9cc127f379ff52
x = utils.UUIDShort()
fmt.Println(x)  // Mw4hP7t9bnMMczU2AvyorU
x = xid.NewString()
fmt.Println(x)  // c294bsnn5ek0ub0200fg

x = base58.Encode([]byte("Test data"))
fmt.Println(x)  // 25JnwSn7XKfNQ
x = utils.B2S(base58.Decode("25JnwSn7XKfNQ"))
fmt.Println(x)  // Test data

choice :=utils.WeightedChoice([]utils.TChoice{
    {"A", 5},
    {"B", 3},
    {"C", 2},
    {"D", 0},
}...)
fmt.Println(choice.String())  // {"Item":"B","Weight":3}

items := []interface{}{"Item.1", "Item.2", "Item.3", "Item.4"}
weights := []int{1, 2, 3, 100}
idx := utils.WeightedChoiceWeightsIndex(weights)
fmt.Println(items[idx])  // Item.4

itemMap := map[interface{}]int{"Item.1": 1, "Item.2": 2, "Item.3": 3, "Item.4": 100}
item := utils.WeightedChoiceMap(itemMap)
fmt.Println(item)  // Item.4

whoami := utils.Executable(true)
whereami := utils.ExecutableDir(true)

fmt.Println(utils.HumanBytes(1234567890)) // 1.2 GB
fmt.Println(utils.Commaf(1234567890.123)) // 1,234,567,890.123

now := time.Date(2020, 2, 18, 12, 13, 14, 123456789, time.UTC)
fmt.Println(utils.Get0LastWeek(now).Format(time.RFC3339Nano)) // 2020-02-10T00:00:00Z

fmt.Println(utils.GetNotInternalIPv4String("100.125.1.1", "", true))  // 100.125.1.1
fmt.Println(utils.GetNotInternalIPv4String("100.125.1.1", "1.2.3.4")) // 1.2.3.4
fmt.Println(utils.GetNotInternalIPv4String("192.168.1.1", "1.2.3.4")) // 1.2.3.4
fmt.Println(utils.GetNotInternalIPv4String("119.118.7.6", "1.2.3.4")) // 119.118.7.6

...

ff

Documentation

Index

Constants

View Source
const (
	Byte = 1 << (iota * 10)
	KiByte
	MiByte
	GiByte
	TiByte
	PiByte
	EiByte
)

Ref: dustin/go-humanize IEC Sizes. kibis of bits

View Source
const (
	IByte = 1
	KByte = IByte * 1000
	MByte = KByte * 1000
	GByte = MByte * 1000
	TByte = GByte * 1000
	PByte = TByte * 1000
	EByte = PByte * 1000
)

SI Sizes.

Variables

This section is empty.

Functions

func AddString

func AddString(s ...string) string

拼接字符串

func AddStringBytes

func AddStringBytes(s ...string) []byte

拼接字符串, 返回 bytes from bytes.Join()

func AesCBCDeB58 added in v0.0.3

func AesCBCDeB58(s, key string) []byte

AES 解密, ZerosPadding

func AesCBCDeB64

func AesCBCDeB64(s, key string) []byte

AES 解密, ZerosPadding

func AesCBCDeHex

func AesCBCDeHex(s, key string) []byte

AES 解密, ZerosPadding

func AesCBCDePKCS7B58 added in v0.0.3

func AesCBCDePKCS7B58(s, key string) []byte

AES 解密, Pkcs7Padding

func AesCBCDePKCS7B64

func AesCBCDePKCS7B64(s, key string) []byte

AES 解密, Pkcs7Padding

func AesCBCDePKCS7Hex

func AesCBCDePKCS7Hex(s, key string) []byte

AES 解密, Pkcs7Padding

func AesCBCDePKCS7StringB58 added in v0.0.3

func AesCBCDePKCS7StringB58(s, key string) string

AES 解密, Pkcs7Padding

func AesCBCDePKCS7StringB64

func AesCBCDePKCS7StringB64(s, key string) string

AES 解密, Pkcs7Padding

func AesCBCDePKCS7StringHex

func AesCBCDePKCS7StringHex(s, key string) string

AES 解密, Pkcs7Padding

func AesCBCDeStringB58 added in v0.0.3

func AesCBCDeStringB58(s, key string) string

AES 解密, ZerosPadding

func AesCBCDeStringB64

func AesCBCDeStringB64(s, key string) string

AES 解密, ZerosPadding

func AesCBCDeStringHex

func AesCBCDeStringHex(s, key string) string

AES 解密, ZerosPadding

func AesCBCDecrypt

func AesCBCDecrypt(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) (plaintext []byte)

AES-CBC 解密

func AesCBCDecryptE added in v0.0.3

func AesCBCDecryptE(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) ([]byte, error)

AES-CBC 解密, 密码分组链接模式 (Cipher Block Chaining (CBC))

func AesCBCEnB58 added in v0.0.3

func AesCBCEnB58(b, key []byte) string

AES 加密, ZerosPadding

func AesCBCEnB64

func AesCBCEnB64(b, key []byte) string

AES 加密, ZerosPadding

func AesCBCEnHex

func AesCBCEnHex(b, key []byte) string

AES 加密, ZerosPadding

func AesCBCEnPKCS7B58 added in v0.0.3

func AesCBCEnPKCS7B58(b, key []byte) string

AES 加密, Pkcs7Padding

func AesCBCEnPKCS7B64

func AesCBCEnPKCS7B64(b, key []byte) string

AES 加密, Pkcs7Padding

func AesCBCEnPKCS7Hex

func AesCBCEnPKCS7Hex(b, key []byte) string

AES 加密, Pkcs7Padding

func AesCBCEnPKCS7StringB58 added in v0.0.3

func AesCBCEnPKCS7StringB58(s, key string) string

AES 加密, Pkcs7Padding

func AesCBCEnPKCS7StringB64

func AesCBCEnPKCS7StringB64(s, key string) string

AES 加密, Pkcs7Padding

func AesCBCEnPKCS7StringHex

func AesCBCEnPKCS7StringHex(s, key string) string

AES 加密, Pkcs7Padding

func AesCBCEnStringB58 added in v0.0.3

func AesCBCEnStringB58(s, key string) string

AES 加密, ZerosPadding

func AesCBCEnStringB64

func AesCBCEnStringB64(s, key string) string

AES 加密, ZerosPadding

func AesCBCEnStringHex

func AesCBCEnStringHex(s, key string) string

AES 加密, ZerosPadding

func AesCBCEncrypt

func AesCBCEncrypt(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) (ciphertext []byte)

AES-CBC 加密

func AesCBCEncryptE added in v0.0.3

func AesCBCEncryptE(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) ([]byte, error)

AES-CBC 加密, 密码分组链接模式 (Cipher Block Chaining (CBC)) key 长度分别是 16 (AES-128), 24 (AES-192?), 32 (AES-256?) asPKCS7: false (ZerosPadding), true (Pkcs7Padding)

func AesGCMDeB58 added in v0.0.3

func AesGCMDeB58(s, key, nonce string) []byte

AES 解密

func AesGCMDeB64 added in v0.0.3

func AesGCMDeB64(s, key, nonce string) []byte

AES 解密

func AesGCMDeHex added in v0.0.3

func AesGCMDeHex(s, key, nonce string) []byte

AES 解密

func AesGCMDeStringB58 added in v0.0.3

func AesGCMDeStringB58(s, key, nonce string) string

AES 解密

func AesGCMDeStringB64 added in v0.0.3

func AesGCMDeStringB64(s, key, nonce string) string

AES 解密

func AesGCMDeStringHex added in v0.0.3

func AesGCMDeStringHex(s, key, nonce string) string

AES 解密

func AesGCMDecrypt added in v0.0.3

func AesGCMDecrypt(ciphertext, key, nonce []byte) (plaintext []byte)

AES-GCM 解密

func AesGCMDecryptWithNonce added in v0.0.3

func AesGCMDecryptWithNonce(ciphertext, key, nonce, additionalData []byte) ([]byte, error)

AES-GCM 解密, (Galois/Counter Mode (GCM))

func AesGCMEnB58 added in v0.0.3

func AesGCMEnB58(b, key []byte) (string, string)

AES 加密

func AesGCMEnB64 added in v0.0.3

func AesGCMEnB64(b, key []byte) (string, string)

AES 加密

func AesGCMEnHex added in v0.0.3

func AesGCMEnHex(b, key []byte) (string, string)

AES 加密

func AesGCMEnStringB58 added in v0.0.3

func AesGCMEnStringB58(s, key string) (string, string)

AES 加密

func AesGCMEnStringB64 added in v0.0.3

func AesGCMEnStringB64(s, key string) (string, string)

AES 加密

func AesGCMEnStringHex added in v0.0.3

func AesGCMEnStringHex(s, key string) (string, string)

AES 加密

func AesGCMEncrypt added in v0.0.3

func AesGCMEncrypt(plaintext, key []byte) (ciphertext []byte, nonce []byte)

AES-GCM 加密

func AesGCMEncryptWithNonce added in v0.0.3

func AesGCMEncryptWithNonce(plaintext, key, nonce, additionalData []byte) ([]byte, []byte, error)

AES-GCM 加密, (Galois/Counter Mode (GCM)) key 长度分别是 16 (AES-128), 32 (AES-256)

func AssertEqual

func AssertEqual(t testing.TB, expected, actual interface{}, description ...string)

AssertEqual checks if values are equal Ref: gofiber/utils

func AssertPanics

func AssertPanics(t *testing.T, title string, f func())

断言 panic

func B2S

func B2S(b []byte) string

BytesToString

func B64Decode

func B64Decode(s string) []byte

Base64 解码

func B64Encode

func B64Encode(b []byte) string

Base64 编码

func B64UrlDecode

func B64UrlDecode(s string) []byte

Base64 解码

func B64UrlEncode

func B64UrlEncode(b []byte) string

Base64 解码, 安全 URL, 替换: "+/" 为 "-_"

func BigComma added in v0.1.3

func BigComma(b *big.Int) string

big.Int 千分位分隔字符串 Ref: dustin/go-humanize

func BigCommaf added in v0.1.3

func BigCommaf(v *big.Float) string

big.Float 千分位分隔字符串 Ref: dustin/go-humanize

func Bigoom added in v0.1.3

func Bigoom(n, b *big.Int) (float64, int)

big.Int 总数量级 Ref: dustin/go-humanize

func CallPath

func CallPath() string

运行时路径, 编译目录 假如: mklink E:\tmp\linkapp.exe D:\Fufu\Test\abc\app.exe 执行: E:\tmp\linkapp.exe CallPath: E:\Go\src\github.com\fufuok\utils\tmp\osext

func Comma added in v0.1.3

func Comma(v int64) string

整数转千分位分隔字符串 Ref: dustin/go-humanize e.g. Comma(834142) -> 834,142

func Commaf added in v0.1.3

func Commaf(v float64) string

浮点数转千分位分隔字符串 Ref: dustin/go-humanize e.g. Commaf(834142.32) -> 834,142.32

func Commai added in v0.1.4

func Commai(v int) string

整数转千分位分隔字符串

func Commau added in v0.1.3

func Commau(v uint64) string

整数转千分位分隔字符串 Ref: dustin/go-humanize

func CopyB2S added in v0.1.2

func CopyB2S(b []byte) string

Immutable, []byte to string

func CopyBytes

func CopyBytes(b []byte) []byte

Immutable, []byte to []byte

func CopyS2B added in v0.1.2

func CopyS2B(s string) []byte

Immutable, string to []byte

func CopyString

func CopyString(s string) string

Immutable, string to string e.g. fiberParam := utils.CopyString(c.Params("test")) e.g. utils.CopyString(s[500:1000]) // 可以让 s 被 GC 回收

func Decrypt

func Decrypt(value, secret string) string

解密

func DesCBCDeB58 added in v0.0.3

func DesCBCDeB58(s, key string) []byte

DES 解密, ZerosPadding

func DesCBCDeB64

func DesCBCDeB64(s, key string) []byte

DES 解密, ZerosPadding

func DesCBCDeHex

func DesCBCDeHex(s, key string) []byte

DES 解密, ZerosPadding

func DesCBCDePKCS7B58 added in v0.0.3

func DesCBCDePKCS7B58(s, key string) []byte

DES 解密, Pkcs7Padding

func DesCBCDePKCS7B64

func DesCBCDePKCS7B64(s, key string) []byte

DES 解密, Pkcs7Padding

func DesCBCDePKCS7Hex

func DesCBCDePKCS7Hex(s, key string) []byte

DES 解密, Pkcs7Padding

func DesCBCDePKCS7StringB58 added in v0.0.3

func DesCBCDePKCS7StringB58(s, key string) string

DES 解密, Pkcs7Padding

func DesCBCDePKCS7StringB64

func DesCBCDePKCS7StringB64(s, key string) string

DES 解密, Pkcs7Padding

func DesCBCDePKCS7StringHex

func DesCBCDePKCS7StringHex(s, key string) string

DES 解密, Pkcs7Padding

func DesCBCDeStringB58 added in v0.0.3

func DesCBCDeStringB58(s, key string) string

DES 解密, ZerosPadding

func DesCBCDeStringB64

func DesCBCDeStringB64(s, key string) string

DES 解密, ZerosPadding

func DesCBCDeStringHex

func DesCBCDeStringHex(s, key string) string

DES 解密, ZerosPadding

func DesCBCDecrypt

func DesCBCDecrypt(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) (plaintext []byte)

AES-CBC 解密

func DesCBCDecryptE added in v0.0.3

func DesCBCDecryptE(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) ([]byte, error)

DES-CBC 解密, 密码分组链接模式 (Cipher Block Chaining (CBC))

func DesCBCEnB58 added in v0.0.3

func DesCBCEnB58(b, key []byte) string

DES 加密, ZerosPadding

func DesCBCEnB64

func DesCBCEnB64(b, key []byte) string

DES 加密, ZerosPadding

func DesCBCEnHex

func DesCBCEnHex(b, key []byte) string

DES 加密, ZerosPadding

func DesCBCEnPKCS7B58 added in v0.0.3

func DesCBCEnPKCS7B58(b, key []byte) string

DES 加密, Pkcs7Padding

func DesCBCEnPKCS7B64

func DesCBCEnPKCS7B64(b, key []byte) string

DES 加密, Pkcs7Padding

func DesCBCEnPKCS7Hex

func DesCBCEnPKCS7Hex(b, key []byte) string

DES 加密, Pkcs7Padding

func DesCBCEnPKCS7StringB58 added in v0.0.3

func DesCBCEnPKCS7StringB58(s, key string) string

DES 加密, Pkcs7Padding

func DesCBCEnPKCS7StringB64

func DesCBCEnPKCS7StringB64(s, key string) string

DES 加密, Pkcs7Padding

func DesCBCEnPKCS7StringHex

func DesCBCEnPKCS7StringHex(s, key string) string

DES 加密, Pkcs7Padding

func DesCBCEnStringB58 added in v0.0.3

func DesCBCEnStringB58(s, key string) string

DES 加密, ZerosPadding

func DesCBCEnStringB64

func DesCBCEnStringB64(s, key string) string

DES 加密, ZerosPadding

func DesCBCEnStringHex

func DesCBCEnStringHex(s, key string) string

DES 加密, ZerosPadding

func DesCBCEncrypt

func DesCBCEncrypt(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) (ciphertext []byte)

AES-CBC 加密

func DesCBCEncryptE added in v0.0.3

func DesCBCEncryptE(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) ([]byte, error)

DES-CBC 加密, 密码分组链接模式 (Cipher Block Chaining (CBC)) key 长度固定为 8 asPKCS7: false (ZerosPadding), true (Pkcs7Padding)

func EncodeUUID added in v0.0.3

func EncodeUUID(id []byte) []byte

编码 UUID

func Encrypt

func Encrypt(value, secret string) string

加密 (密钥取 32 位 MD5, AES-CBC, base58)

func Executable added in v0.1.2

func Executable(evalSymlinks ...bool) string

当前执行程序绝对路径 true 时返回解析符号链接后的绝对路径 Excutable: E:\tmp\linkapp.exe Excutable(true): D:\Fufu\Test\abc\app.exe

func ExecutableDir added in v0.1.2

func ExecutableDir(evalSymlinks ...bool) string

当前执行程序所在目录 true 时返回解析符号链接后的目录 ExcutableDir: E:\tmp ExcutableDir(true): D:\Fufu\Test\abc

func Get0Hour added in v0.1.5

func Get0Hour(t time.Time) time.Time

当天 0 点

func Get0LastMonth added in v0.1.5

func Get0LastMonth(t time.Time) time.Time

上月第一天 0 点

func Get0LastWeek added in v0.1.5

func Get0LastWeek(t time.Time) time.Time

上周一 0 点

func Get0Minute added in v0.1.5

func Get0Minute(t time.Time) time.Time

0 分

func Get0Month added in v0.1.5

func Get0Month(t time.Time) time.Time

当月第一天 0 点

func Get0NextMonth added in v0.1.5

func Get0NextMonth(t time.Time) time.Time

下月第一天 0 点

func Get0NextWeek added in v0.1.5

func Get0NextWeek(t time.Time) time.Time

下周一 0 点

func Get0Second added in v0.1.5

func Get0Second(t time.Time) time.Time

0 秒

func Get0Tomorrow added in v0.1.5

func Get0Tomorrow(t time.Time) time.Time

昨天 0 点

func Get0Week added in v0.1.5

func Get0Week(t time.Time) time.Time

本周一 0 点

func Get0Yesterday added in v0.1.5

func Get0Yesterday(t time.Time) time.Time

昨天 0 点

func GetBytes

func GetBytes(v interface{}, defaultVal ...[]byte) []byte

先转为字符串再转为 []byte, 可选指定默认值

func GetInt

func GetInt(v interface{}, defaultInt ...int) int

获取 int 结果, 可选指定默认值(若给定了默认值,则返回正整数或 0)

func GetMonthDays added in v0.1.5

func GetMonthDays(t time.Time) int

当月天数

func GetNotInternalIPv4 added in v0.1.5

func GetNotInternalIPv4(ip, defaultIP net.IP, flag ...bool) net.IP

如果是内网 IPv4 则使用默认值, flag 为真是必定返回一个 IP

func GetNotInternalIPv4String added in v0.1.5

func GetNotInternalIPv4String(ip, defaultIP string, flag ...bool) string

如果是内网 IPv4 则使用默认值

func GetSafeB2S added in v0.1.1

func GetSafeB2S(b []byte, defaultVal ...string) string

Immutable, 可选指定默认值

func GetSafeBytes added in v0.1.1

func GetSafeBytes(b []byte, defaultVal ...[]byte) []byte

Immutable, 可选指定默认值

func GetSafeS2B added in v0.1.1

func GetSafeS2B(s string, defaultVal ...[]byte) []byte

Immutable, 可选指定默认值

func GetSafeString added in v0.1.1

func GetSafeString(s string, defaultVal ...string) string

Immutable, 可选指定默认值

func GetString

func GetString(v interface{}, defaultVal ...string) string

获取字符串结果, 可选指定默认值

func GetenvDecrypt

func GetenvDecrypt(key string, secret string) string

解密环境变量参数

func Hash

func Hash(b []byte, h hash.Hash) []byte

Hash

func Hmac

func Hmac(b []byte, key []byte, h func() hash.Hash) []byte

Hmac

func HmacSHA1

func HmacSHA1(b, key []byte) []byte

HmacSHA1

func HmacSHA1Hex

func HmacSHA1Hex(s, key string) string

HmacSHA1Hex

func HmacSHA256

func HmacSHA256(b, key []byte) []byte

HmacSHA256

func HmacSHA256Hex

func HmacSHA256Hex(s, key string) string

HmacSHA256Hex

func HmacSHA512

func HmacSHA512(b, key []byte) []byte

HmacSHA512

func HmacSHA512Hex

func HmacSHA512Hex(s, key string) string

HmacSHA512Hex

func HumanBaseBytes added in v0.1.3

func HumanBaseBytes(v uint64, base float64, sizes []string) string

数字的数量级表示

func HumanBytes added in v0.1.3

func HumanBytes(v uint64) string

数字的数量级表示 e.g. HumanBytes(82854982) -> 83 MB

func HumanIBytes added in v0.1.3

func HumanIBytes(v uint64) string

数字的数量级表示 e.g. HumanIBytes(82854982) -> 79 MiB

func HumanIntBytes added in v0.1.4

func HumanIntBytes(v int) string

数字的数量级表示

func HumanIntIBytes added in v0.1.4

func HumanIntIBytes(v int) string

数字的数量级表示

func IPv42Long added in v0.1.5

func IPv42Long(ip net.IP) int

IPv4 转数值

func IPv4String2Long added in v0.1.5

func IPv4String2Long(ip string) int

IPv4 字符串转数值

func InInts

func InInts(slice []int, n int) bool

检查整数是否存在于 slice

func InStrings

func InStrings(ss []string, s string) bool

检查字符串是否存在于 slice

func IsDir

func IsDir(s string) bool

目录是否存在

func IsExist

func IsExist(s string) bool

文件或目录是否存在

func IsFile

func IsFile(s string) bool

文件是否存在

func IsInternalIPv4 added in v0.1.5

func IsInternalIPv4(ip net.IP) bool

是否为内网 IPv4, 包含 NAT 专用网段 RFC6598, 比如华为云 ELB 的 100.125.0.0/16

func IsInternalIPv4String added in v0.1.5

func IsInternalIPv4String(ip string) bool

是否为内网 IPv4

func JoinBytes

func JoinBytes(b ...[]byte) []byte

拼接 []byte

func Logn added in v0.1.3

func Logn(n, b float64) float64

func Long2IPv4 added in v0.1.5

func Long2IPv4(n int) net.IP

数值转 IPv4

func Long2IPv4String added in v0.1.5

func Long2IPv4String(n int) string

数值转 IPv4 字符串

func MD5

func MD5(b []byte) []byte

MD5

func MD5Hex

func MD5Hex(s string) string

字符串 MD5

func MD5Reader

func MD5Reader(r io.Reader) (string, error)

计算 MD5

func MD5Sum

func MD5Sum(filename string) (string, error)

文件 MD5

func MaxInt

func MaxInt(a, b int) int

整数取大值

func MinInt

func MinInt(a, b int) int

整数取小值

func MustBool

func MustBool(v interface{}) bool

强制转为 bool

func MustInt

func MustInt(v interface{}) int

强制转为整数 (int)

func MustJSON

func MustJSON(v interface{}) []byte

转 JSON 返回 []byte

func MustJSONString

func MustJSONString(v interface{}) string

转 JSON 返回 string

func MustString

func MustString(v interface{}) string

强制转为字符串

func Padding

func Padding(b []byte, bSize int, pkcs7 bool) []byte

填充

func ParseHumanBytes added in v0.1.3

func ParseHumanBytes(s string) (uint64, error)

解析数字的数量级表示 e.g. ParseBytes("42 MB") -> 42000000, nil e.g. ParseBytes("42 mib") -> 44040192, nil

func RandBytes added in v0.0.3

func RandBytes(n int) []byte

随机 bytes

func RandHex added in v0.0.3

func RandHex(nHalf int) string

随机 Hex 编码字符串

func RandInt

func RandInt(min int, max int) int

(>=)m - (<)n 间随机整数

func RandString

func RandString(n int) string

随机字符串, 大小写字母数字 Ref: stackoverflow.icza

func Round

func Round(v float64, precision int) float64

Round 四舍五入, ROUND_HALF_UP 模式实现 返回将 val 根据指定精度 precision (十进制小数点后数字的数目) 进行四舍五入的结果 precision 也可以是负数或零 Ref: thinkeridea/go-extend

func RunPath

func RunPath() string

实际程序所在目录 RunPath: E:\tmp

func S2B

func S2B(s string) []byte

StringToBytes converts string to byte slice without a memory allocation. Ref: gin

func SearchInt

func SearchInt(slice []int, n int) int

搜索整数位置(左, 第一个)

func SearchString

func SearchString(ss []string, s string) int

搜索字符串位置(左, 第一个)

func SetenvEncrypt

func SetenvEncrypt(key, value, secret string) (string, error)

加密并设置环境变量

func Sha1

func Sha1(b []byte) []byte

Sha1

func Sha1Hex

func Sha1Hex(s string) string

Sha1Hex

func Sha256

func Sha256(b []byte) []byte

Sha256

func Sha256Hex

func Sha256Hex(s string) string

Sha256Hex

func Sha512

func Sha512(b []byte) []byte

Sha512

func Sha512Hex

func Sha512Hex(s string) string

Sha512Hex

func Str2Bytes added in v0.0.3

func Str2Bytes(s string) (b []byte)

Ref: csdn.weixin_43705457

func StrToBytes added in v0.1.3

func StrToBytes(s string) []byte

Ref: Allenxuxu / toolkit

func String2Bytes added in v0.0.3

func String2Bytes(s string) (b []byte)

Ref: fasthttp

func StringToBytes added in v0.0.3

func StringToBytes(s string) (b []byte)

Ref: csdn.u010853261

func UUID added in v0.0.3

func UUID() []byte

随机 UUID, RFC4122, Version 4

func UUIDShort added in v0.0.3

func UUIDShort() string

随机 UUID, 短版, base58

func UUIDSimple added in v0.0.3

func UUIDSimple() string

随机 UUID, 无短横线

func UUIDString added in v0.0.3

func UUIDString() string

随机 UUID

func UnPadding

func UnPadding(b []byte, pkcs7 bool) []byte

去除填充

func WaitNextMinute

func WaitNextMinute()

下一分钟, 对齐时间, 0 秒

func WeightedChoiceIndex added in v0.0.3

func WeightedChoiceIndex(choices []TChoice) int

加权随机, 返回选中项的下标, -1 未选中任何项

func WeightedChoiceMap added in v0.0.3

func WeightedChoiceMap(choices map[interface{}]int) interface{}

加权随机, map[object]weight

func WeightedChoiceWeightsIndex added in v0.0.3

func WeightedChoiceWeightsIndex(weights []int) int

加权随机, 参数为权重值列表, 返回选中项的下标, -1 未选中任何项

Types

type TChoice added in v0.0.3

type TChoice struct {
	Item   interface{}
	Weight int
}

func WeightedChoice added in v0.0.3

func WeightedChoice(choices ...TChoice) TChoice

加权随机, 返回选中项

func (*TChoice) String added in v0.0.3

func (c *TChoice) String() string

Directories

Path Synopsis
Package base58 provides an API for working with modified base58 and Base58Check encodings.
Package base58 provides an API for working with modified base58 and Base58Check encodings.
环境变量加密工具 go run main.go -d=Fufu go run main.go -d="Fufu 777" go run main.go -d=Fufu -k=TestEnv go run main.go -k=TestEnv
环境变量加密工具 go run main.go -d=Fufu go run main.go -d="Fufu 777" go run main.go -d=Fufu -k=TestEnv go run main.go -k=TestEnv
Package xid is a globally unique id generator suited for web scale Xid is using Mongo Object ID algorithm to generate globally unique ids: https://docs.mongodb.org/manual/reference/object-id/ - 4-byte value representing the seconds since the Unix epoch, - 3-byte machine identifier, - 2-byte process id, and - 3-byte counter, starting with a random value.
Package xid is a globally unique id generator suited for web scale Xid is using Mongo Object ID algorithm to generate globally unique ids: https://docs.mongodb.org/manual/reference/object-id/ - 4-byte value representing the seconds since the Unix epoch, - 3-byte machine identifier, - 2-byte process id, and - 3-byte counter, starting with a random value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL