commit
4bfe4519ad
@ -0,0 +1,7 @@
|
||||
.env
|
||||
.git
|
||||
.svn
|
||||
.idea
|
||||
.vscode
|
||||
*.log
|
||||
git.sh
|
@ -0,0 +1,15 @@
|
||||
<img align="right" width="100" src="https://kodo-cdn.dtapp.net/04/999e9f2f06d396968eacc10ce9bc8a.png" alt="www.dtapp.net"/>
|
||||
|
||||
<h1 align="left"><a href="https://www.dtapp.net/">Golang扩展包</a></h1>
|
||||
|
||||
📦 Golang扩展包
|
||||
|
||||
### 安装
|
||||
```text
|
||||
go get gitee.com/dtapps/GoLibrary
|
||||
```
|
||||
|
||||
### 安装
|
||||
```text
|
||||
go get github.com/dtapps/GoLibrary
|
||||
```
|
@ -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)]
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package decimal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Decimal(value float64) float64 {
|
||||
value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
|
||||
return value
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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))
|
||||
}
|
@ -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
|
||||
}
|
@ -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")
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package uuid
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
func GenUUID() string {
|
||||
u, _ := uuid.NewRandom()
|
||||
return u.String()
|
||||
}
|
Loading…
Reference in new issue