This commit is contained in:
wangchuxiao
2022-06-09 17:19:39 +08:00
parent c46f574aa8
commit ab76d46007
7 changed files with 84 additions and 22 deletions
+1
View File
@@ -6,6 +6,7 @@ type Register struct {
Account string `gorm:"column:account;primary_key;type:char(255)" json:"account"`
Password string `gorm:"column:password;type:varchar(255)" json:"password"`
Ex string `gorm:"column:ex;size:1024" json:"ex"`
UserID string `gorm:"column:user_id;type:varchar(255)" json:"userID"`
}
//
@@ -15,11 +15,12 @@ func GetRegister(account string) (*db.Register, error) {
account).Take(&r).Error
}
func SetPassword(account, password, ex string) error {
func SetPassword(account, password, ex, userID string) error {
r := db.Register{
Account: account,
Password: password,
Ex: ex,
UserID: userID,
}
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
+64
View File
@@ -0,0 +1,64 @@
package utils
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/hex"
"errors"
)
func Md5(s string, salt ...string) string {
h := md5.New()
h.Write([]byte(s))
if len(salt) > 0 {
h.Write([]byte(salt[0]))
}
cipher := h.Sum(nil)
return hex.EncodeToString(cipher)
}
func AesEncrypt(data []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
encryptBytes := pkcs7Padding(data, blockSize)
crypted := make([]byte, len(encryptBytes))
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
blockMode.CryptBlocks(crypted, encryptBytes)
return crypted, nil
}
func AesDecrypt(data []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
crypted := make([]byte, len(data))
blockMode.CryptBlocks(crypted, data)
crypted, err = pkcs7UnPadding(crypted)
if err != nil {
return nil, err
}
return crypted, nil
}
func pkcs7Padding(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padText...)
}
func pkcs7UnPadding(data []byte) ([]byte, error) {
length := len(data)
if length == 0 {
return nil, errors.New("encrypt error")
}
unPadding := int(data[length-1])
return data[:(length - unPadding)], nil
}
-13
View File
@@ -1,13 +0,0 @@
package utils
import (
"crypto/md5"
"encoding/hex"
)
func Md5(s string) string {
h := md5.New()
h.Write([]byte(s))
cipher := h.Sum(nil)
return hex.EncodeToString(cipher)
}