commit 4bfe4519ad2dfd6e875b9eb43a62c44e3553e9f4 Author: 李光春 Date: Fri May 28 22:53:43 2021 +0800 GoLibrary diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f86fc9d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.env +.git +.svn +.idea +.vscode +*.log +git.sh \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 00000000..1dc431bc --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +www.dtapp.net + +

Golang扩展包

+ +📦 Golang扩展包 + +### 安装 +```text +go get gitee.com/dtapps/GoLibrary +``` + +### 安装 +```text +go get github.com/dtapps/GoLibrary +``` diff --git a/helper/aes/aes.go b/helper/aes/aes.go new file mode 100644 index 00000000..492c4e1e --- /dev/null +++ b/helper/aes/aes.go @@ -0,0 +1,57 @@ +package aes + +import ( + "bytes" + "crypto/aes" + "crypto/cipher" + "encoding/base64" +) + +// Encrypt 加密 aes_128_cbc +func Encrypt(encryptStr string, key []byte, iv string) (string, error) { + encryptBytes := []byte(encryptStr) + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + blockSize := block.BlockSize() + encryptBytes = pkcs5Padding(encryptBytes, blockSize) + + blockMode := cipher.NewCBCEncrypter(block, []byte(iv)) + encrypted := make([]byte, len(encryptBytes)) + blockMode.CryptBlocks(encrypted, encryptBytes) + return base64.URLEncoding.EncodeToString(encrypted), nil +} + +// Decrypt 解密 +func Decrypt(decryptStr string, key []byte, iv string) (string, error) { + decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr) + if err != nil { + return "", err + } + + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + blockMode := cipher.NewCBCDecrypter(block, []byte(iv)) + decrypted := make([]byte, len(decryptBytes)) + + blockMode.CryptBlocks(decrypted, decryptBytes) + decrypted = pkcs5UnPadding(decrypted) + return string(decrypted), nil +} + +func pkcs5Padding(cipherText []byte, blockSize int) []byte { + padding := blockSize - len(cipherText)%blockSize + padText := bytes.Repeat([]byte{byte(padding)}, padding) + return append(cipherText, padText...) +} + +func pkcs5UnPadding(decrypted []byte) []byte { + length := len(decrypted) + unPadding := int(decrypted[length-1]) + return decrypted[:(length - unPadding)] +} diff --git a/helper/decimal/decimal.go b/helper/decimal/decimal.go new file mode 100644 index 00000000..46fa5cf4 --- /dev/null +++ b/helper/decimal/decimal.go @@ -0,0 +1,11 @@ +package decimal + +import ( + "fmt" + "strconv" +) + +func Decimal(value float64) float64 { + value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64) + return value +} diff --git a/helper/json/json.go b/helper/json/json.go new file mode 100644 index 00000000..86515469 --- /dev/null +++ b/helper/json/json.go @@ -0,0 +1,11 @@ +package json + +import "encoding/json" + +func Encode(v interface{}) (string, error) { + bytes, err := json.Marshal(v) + if err != nil { + return "", err + } + return string(bytes), nil +} diff --git a/helper/mail/mail.go b/helper/mail/mail.go new file mode 100644 index 00000000..b89f0943 --- /dev/null +++ b/helper/mail/mail.go @@ -0,0 +1,43 @@ +package mail + +import ( + "fmt" + "gopkg.in/gomail.v2" + "strings" +) + +type Options struct { + MailHost string + MailPort int + MailUser string // 发件人 + MailPass string // 发件人密码 + MailTo string // 收件人 多个用,分割 + Subject string // 邮件主题 + Body string // 邮件内容 +} + +func Send(o *Options) error { + + m := gomail.NewMessage() + + //设置发件人 + m.SetHeader("From", o.MailUser) + + //设置发送给多个用户 + mailArrTo := strings.Split(o.MailTo, ",") + m.SetHeader("To", mailArrTo...) + + //设置邮件主题 + m.SetHeader("Subject", o.Subject) + + //设置邮件正文 + m.SetBody("text/html", o.Body) + + d := gomail.NewDialer(o.MailHost, o.MailPort, o.MailUser, o.MailPass) + + err := d.DialAndSend(m) + if err != nil { + fmt.Println(err) + } + return err +} diff --git a/helper/md5/md5.go b/helper/md5/md5.go new file mode 100644 index 00000000..82f9d70b --- /dev/null +++ b/helper/md5/md5.go @@ -0,0 +1,12 @@ +package md5 + +import ( + "crypto/md5" + "encoding/hex" +) + +func MD5(str string) string { + s := md5.New() + s.Write([]byte(str)) + return hex.EncodeToString(s.Sum(nil)) +} diff --git a/helper/rsa/rsa.go b/helper/rsa/rsa.go new file mode 100644 index 00000000..891be13f --- /dev/null +++ b/helper/rsa/rsa.go @@ -0,0 +1,77 @@ +package rsa + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "encoding/pem" + "os" +) + +// PublicEncrypt 公钥加密 +func PublicEncrypt(encryptStr string, path string) (string, error) { + // 打开文件 + file, err := os.Open(path) + if err != nil { + return "", err + } + defer file.Close() + + // 读取文件内容 + info, _ := file.Stat() + buf := make([]byte, info.Size()) + file.Read(buf) + + // pem 解码 + block, _ := pem.Decode(buf) + + // x509 解码 + publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes) + if err != nil { + return "", err + } + + // 类型断言 + publicKey := publicKeyInterface.(*rsa.PublicKey) + + //对明文进行加密 + encryptedStr, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(encryptStr)) + if err != nil { + return "", err + } + + //返回密文 + return base64.URLEncoding.EncodeToString(encryptedStr), nil +} + +// PrivateDecrypt 私钥解密 +func PrivateDecrypt(decryptStr string, path string) (string, error) { + // 打开文件 + file, err := os.Open(path) + if err != nil { + return "", err + } + defer file.Close() + + // 获取文件内容 + info, _ := file.Stat() + buf := make([]byte, info.Size()) + file.Read(buf) + + // pem 解码 + block, _ := pem.Decode(buf) + + // X509 解码 + privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) + if err != nil { + return "", err + } + decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr) + + //对密文进行解密 + decrypted, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, decryptBytes) + + //返回明文 + return string(decrypted), nil +} diff --git a/helper/time/time.go b/helper/time/time.go new file mode 100644 index 00000000..eae49027 --- /dev/null +++ b/helper/time/time.go @@ -0,0 +1,28 @@ +package time + +import "time" + +// GetCurrentDate 获取当前的时间 - 字符串 +func GetCurrentDate() string { + return time.Now().Format("2006/01/02 15:04:05") +} + +// GetCurrentUnix 获取当前的时间 - Unix时间戳 +func GetCurrentUnix() int64 { + return time.Now().Unix() +} + +// GetCurrentMilliUnix 获取当前的时间 - 毫秒级时间戳 +func GetCurrentMilliUnix() int64 { + return time.Now().UnixNano() / 1000000 +} + +// GetCurrentNanoUnix 获取当前的时间 - 纳秒级时间戳 +func GetCurrentNanoUnix() int64 { + return time.Now().UnixNano() +} + +// GetCurrentWjDate 获取当前的时间 - 字符串 - 没有间隔 +func GetCurrentWjDate() string { + return time.Now().Format("20060102") +} diff --git a/helper/uuid/uuid.go b/helper/uuid/uuid.go new file mode 100644 index 00000000..083faae6 --- /dev/null +++ b/helper/uuid/uuid.go @@ -0,0 +1,8 @@ +package uuid + +import "github.com/google/uuid" + +func GenUUID() string { + u, _ := uuid.NewRandom() + return u.String() +}