138 lines
3.6 KiB
Go
138 lines
3.6 KiB
Go
package test
|
||
|
||
import (
|
||
"crypto/rand"
|
||
"crypto/rsa"
|
||
"crypto/x509"
|
||
"encoding/base64"
|
||
"encoding/pem"
|
||
"fmt"
|
||
"go-admin/app/admin/models/bus_models"
|
||
"go-admin/tools/crypto"
|
||
"io"
|
||
"os"
|
||
|
||
"testing"
|
||
//"text/template"
|
||
)
|
||
|
||
func TestGoModelTemplate(t *testing.T) {
|
||
//t1, err := template.ParseFiles("model.go.template")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//table := tools.SysTables{}
|
||
//table.TBName = "sys_tables"
|
||
//tab, err := table.Get()
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//file, err := os.Create("models/" + table.PackageName + ".go")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//defer file.Close()
|
||
//
|
||
//_ = t1.Execute(file, tab)
|
||
//t.Log("")
|
||
|
||
encryptedText := "Uo++o250R//CB0qbuTH2/DpbIEPDccCzAcXQPB+Yg86KuXil5/BJRioIFNBJkfD0O7CJoV6s/vHp8MoTazEteWmBb8AS31tYxfEfYCvWl6sLTUXhvEQLBOZRjWSXRMlHTF4EXnrio7Ga3UQJ7C9B62lhvLWCG42AK8niTswx0DbgHHCXw1gS7vXq/bhs1K6JlNA1fLKW23SqwaIKJ7dSUdEyUkpv649RXAQK6T1kKoKwh6fqY6+J4H17z1KTDHaVHX11NPmCtZYVWEg8Q10uRM0FqxHd7jTKfMfDlpc/xBacUVhA0QC/VJEkkVs+Bm4ZXi5HghKgRjBiRv1bpvInj+TvkpR83iB7Y5gMwS+1RfdfkZ8pqjMzOuQEDLLRmDIvLyCwYTjZWXcMsO/C1POWg/JyRKAK3kGKkXe1LyLBTSzEDfa8c28LkCNMDgHYW4g9r1bZ5m4H/27/RcQEkT1TRiIHYS0fFqiXt7jcr3GWqT8ES5k/Y6MRheXB2SauiQYueauS6e487cDlzMf245Tw1lkJmn+Yg0Byr2O2IIvx9TGroqstDYWwWbc6NtyLL744fsZW+RZ9N1e41+T3kJUFP++RJWIXvIvZeeVUD+OEgEI="
|
||
// 解密
|
||
decryptedText, err := crypto.AESDecryptJson(bus_models.AESKey, encryptedText)
|
||
if err != nil {
|
||
fmt.Println("解密失败:", err)
|
||
return
|
||
}
|
||
fmt.Println("解密后的文本:", decryptedText)
|
||
}
|
||
|
||
func TestGoApiTemplate(t *testing.T) {
|
||
//t1, err := template.ParseFiles("api.go.template")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//table := tools.SysTables{}
|
||
//table.TBName = "sys_tables"
|
||
//tab, _ := table.Get()
|
||
//file, err := os.Create("apis/" + table.PackageName + ".go")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//defer file.Close()
|
||
//
|
||
//_ = t1.Execute(file, tab)
|
||
//t.Log("")
|
||
|
||
dir, err := os.Getwd()
|
||
if err != nil {
|
||
fmt.Println("Error getting current directory:", err)
|
||
}
|
||
fmt.Println("Current working directory:", dir)
|
||
|
||
// 生成 AES 密钥
|
||
aesKey, err := GenerateAESKey()
|
||
if err != nil {
|
||
fmt.Printf("生成 AES 密钥失败: %v", err)
|
||
}
|
||
|
||
// 打印 AES 密钥
|
||
fmt.Printf("生成的 AES 密钥: %x\n", aesKey)
|
||
|
||
publicKeyPath := "/Users/max/Documents/code/deovo/telco_server/config/sms/public.pem"
|
||
|
||
// 使用公钥加密 AES 密钥
|
||
encryptedKey, err := EncryptWithRSA(publicKeyPath, aesKey)
|
||
if err != nil {
|
||
fmt.Printf("加密 AES 密钥失败: %v", err)
|
||
}
|
||
|
||
// 打印加密后的 AES 密钥(Base64 编码)
|
||
fmt.Printf("加密后的 AES 密钥(Base64 编码): %s\n", encryptedKey)
|
||
}
|
||
|
||
// GenerateAESKey 生成随机的 AES 密钥
|
||
func GenerateAESKey() ([]byte, error) {
|
||
// 生成 256 位(32 字节)的 AES 密钥
|
||
key := make([]byte, 32)
|
||
_, err := rand.Read(key)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return key, nil
|
||
}
|
||
|
||
// EncryptWithRSA 公钥加密 AES 密钥
|
||
func EncryptWithRSA(publicKeyPath string, aesKey []byte) (string, error) {
|
||
// 读取公钥文件
|
||
pubFile, err := os.Open(publicKeyPath)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
defer pubFile.Close()
|
||
|
||
// 解析公钥
|
||
pubBytes, err := io.ReadAll(pubFile)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
block, _ := pem.Decode(pubBytes)
|
||
if block == nil {
|
||
return "", fmt.Errorf("failed to parse PEM block containing the public key")
|
||
}
|
||
|
||
pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
// 使用公钥加密 AES 密钥
|
||
encryptedKey, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, aesKey)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
// 返回加密后的密钥(base64 编码)
|
||
return base64.StdEncoding.EncodeToString(encryptedKey), nil
|
||
}
|