package utils import ( "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/base64" "errors" r "math/rand" "time" ) func AESGCMEncrypt(key, plaintext []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } gcm, err := cipher.NewGCM(block) if err != nil { return nil, err } nonce := make([]byte, gcm.NonceSize()) if _, err := rand.Read(nonce); err != nil { return nil, err } ciphertext := gcm.Seal(nil, nonce, plaintext, nil) return append(nonce, ciphertext...), nil } // AESGCMDecrypt decrypts ciphertext with the given key using AES in GCM mode. func AESGCMDecrypt(key, ciphertext []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } gcm, err := cipher.NewGCM(block) if err != nil { return nil, err } size := gcm.NonceSize() if len(ciphertext)-size <= 0 { return nil, errors.New("Ciphertext is empty") } nonce := ciphertext[:size] ciphertext = ciphertext[size:] plainText, err := gcm.Open(nil, nonce, ciphertext, nil) if err != nil { return nil, err } return plainText, nil } func UrlEncode(data string) string { return base64.URLEncoding.EncodeToString([]byte(data)) } func UrlDecode(data string) (string, error) { res, err := base64.URLEncoding.DecodeString(data) if err != nil { return "", err } return string(res), nil } const ( RAND_KIND_NUM = 0 // 纯数字 RAND_KIND_LOWER = 1 // 小写字母 RAND_KIND_UPPER = 2 // 大写字母 RAND_KIND_ALL = 3 // 数字、大小写字母 ) func RandomStr() string { return ToStr(time.Now().UnixNano()) + string(RandomCreateBytes(10, RAND_KIND_NUM)) } func RandomOrder(prefix string, randomSize int) string { return prefix + DateT(time.Now(), "YYMMDDHHmmss") + ToStr(time.Now().UnixNano())[10:12] + string(RandomCreateBytes(randomSize, RAND_KIND_NUM)) } // RandomCreateBytes generate random []byte by specify chars. func RandomCreateBytes(n int, kind int, alphabets ...byte) []byte { var alphanum = "" switch kind { case RAND_KIND_NUM: alphanum = "0123456789" case RAND_KIND_LOWER: alphanum = "abcdefghijklmnopqrstuvwxyz" case RAND_KIND_UPPER: alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" case RAND_KIND_ALL: alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" } var bytes = make([]byte, n) var randby bool if num, err := rand.Read(bytes); num != n || err != nil { r.Seed(time.Now().UnixNano()) randby = true } for i, b := range bytes { if len(alphabets) == 0 { if randby { bytes[i] = alphanum[r.Intn(len(alphanum))] } else { bytes[i] = alphanum[b%byte(len(alphanum))] } } else { if randby { bytes[i] = alphabets[r.Intn(len(alphabets))] } else { bytes[i] = alphabets[b%byte(len(alphabets))] } } } return bytes } func SubStr(str string, start int, length int) (result string) { s := []rune(str) total := len(s) if total == 0 { return } // 允许从尾部开始计算 if start < 0 { start = total + start if start < 0 { return } } if start > total { return } // 到末尾 if length < 0 { length = total } end := start + length if end > total { result = string(s[start:]) } else { result = string(s[start:end]) } return }