取消明文密码

This commit is contained in:
hahwu 2025-01-02 16:32:08 +08:00
parent e6a87a6c04
commit 4b6e5839ba
3 changed files with 56 additions and 2 deletions

View File

@ -2,8 +2,13 @@ package GoUtil
import (
"bytes"
"crypto/aes"
"crypto/cipher"
crand "crypto/rand"
"encoding/base64"
"encoding/gob"
"fmt"
"io"
"math/rand"
"reflect"
"strconv"
@ -19,6 +24,54 @@ type EventObj struct {
Obj interface{}
}
const (
SECRET_KEY = ")VQbB(vpy=U(wcp)"
)
// 加密字符串
func Encrypt(plainText string) (string, error) {
block, err := aes.NewCipher([]byte(SECRET_KEY))
if err != nil {
return "", err
}
cipherText := make([]byte, aes.BlockSize+len(plainText))
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(crand.Reader, iv); err != nil {
return "", err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(cipherText[aes.BlockSize:], []byte(plainText))
return base64.URLEncoding.EncodeToString(cipherText), nil
}
// 解密字符串
func Decrypt(cipherText string) (string, error) {
cipherTextBytes, err := base64.URLEncoding.DecodeString(cipherText)
if err != nil {
return "", err
}
block, err := aes.NewCipher([]byte(SECRET_KEY))
if err != nil {
return "", err
}
if len(cipherTextBytes) < aes.BlockSize {
return "", fmt.Errorf("cipherText too short")
}
iv := cipherTextBytes[:aes.BlockSize]
cipherTextBytes = cipherTextBytes[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(cipherTextBytes, cipherTextBytes)
return string(cipherTextBytes), nil
}
func (o *EventObj) isEqual(tar *EventObj) bool {
if reflect.ValueOf(o.Callback).Pointer() == reflect.ValueOf(tar.Callback).Pointer() && o.Obj == tar.Obj {
return true

View File

@ -7,7 +7,7 @@
"MySqlAddr": "127.0.0.1",
"MySqlPort": "3306",
"MySqlUsr": "root",
"MySqlPwd": "root",
"MySqlPwd": "IOagNEq3C84c-20CmHEin5iODVc=",
"MaxConnNum": 20000,
"DbName": "Merge_Pet",
"HttpPort": ":8081",

View File

@ -25,7 +25,8 @@ var SqlDb *sqlx.DB
func InitDB() {
//"用户名:密码@[连接方式](主机名:端口号)/数据库名"
connect := fmt.Sprintf("%s:%s@(%s:%s)/%s", conf.Server.MySqlUsr, conf.Server.MySqlPwd, conf.Server.MySqlAddr, conf.Server.MySqlPort, conf.Server.DbName)
MysqlPwd, _ := GoUtil.Decrypt(conf.Server.MySqlPwd)
connect := fmt.Sprintf("%s:%s@(%s:%s)/%s", conf.Server.MySqlUsr, MysqlPwd, conf.Server.MySqlAddr, conf.Server.MySqlPort, conf.Server.DbName)
SqlDb = sqlx.MustConnect("mysql", connect) // 设置连接数据库的参数
SqlDb.SetMaxOpenConns(20) // 设置最大打开的连接数
log.Debug("connect mysql success")