You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.6 KiB
100 lines
2.6 KiB
2 months ago
|
package kms
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
"wallet-system/internal/conf"
|
||
|
"wallet-system/internal/pkg/logging/applogger"
|
||
|
|
||
|
"github.com/aws/aws-sdk-go/aws"
|
||
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||
|
"github.com/aws/aws-sdk-go/aws/session"
|
||
|
"github.com/aws/aws-sdk-go/service/kms"
|
||
|
)
|
||
|
|
||
|
func Test_Testing_Aws_Kms(t *testing.T) {
|
||
|
// 创建自定义的 AWS 配置
|
||
|
awsConfig := &aws.Config{
|
||
|
Region: aws.String("ap-southeast-1"), // 设置 AWS 区域
|
||
|
Credentials: credentials.NewStaticCredentials(
|
||
|
"AKIAUKMLSNHYAP7EOBDE", // 替换为您的访问密钥 ID
|
||
|
"OW1EcVvbuJ2ZDW2X8G1m9K5XIN/KlDgwxNoSOHR5", // 替换为您的访问密钥
|
||
|
""), // 提供一个可选的令牌 (token),如果您使用 MFA
|
||
|
DisableSSL: aws.Bool(false), // 通过 HTTPS 进行连接
|
||
|
S3ForcePathStyle: aws.Bool(true), // 使用路径样式的 URL
|
||
|
HTTPClient: &http.Client{}, // 自定义 HTTP 客户端,可选
|
||
|
}
|
||
|
|
||
|
// 创建 AWS 会话
|
||
|
sess, err := session.NewSession(awsConfig)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建 AWS 会话时出错:", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 创建KMS客户端
|
||
|
kmsClient := kms.New(sess)
|
||
|
|
||
|
// 准备加密的数据
|
||
|
// 钱包:TVBdbsmp67P76gCxGgLa2dHGJFjyXw5X5Z
|
||
|
plainText := []byte("5a79d71f2458d07b7d088bb126c03dd0252e7faa1cc60358830563f74ce508af")
|
||
|
|
||
|
// 指定加密使用的KMS主密钥ID
|
||
|
keyID := "7167910a-2468-4e9f-a1ef-51a7ebee53ae" // 替换为你的KMS主密钥ID
|
||
|
|
||
|
// 使用KMS加密数据
|
||
|
encryptResult, err := kmsClient.Encrypt(&kms.EncryptInput{
|
||
|
KeyId: aws.String(keyID),
|
||
|
Plaintext: plainText,
|
||
|
})
|
||
|
if err != nil {
|
||
|
applogger.Error("使用Kms加密报错:%v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 输出加密后的密文
|
||
|
cipherText := encryptResult.CiphertextBlob
|
||
|
applogger.Debug("加密后的密文:%v", cipherText)
|
||
|
|
||
|
// 使用KMS解密数据
|
||
|
decryptResult, err := kmsClient.Decrypt(&kms.DecryptInput{
|
||
|
CiphertextBlob: cipherText,
|
||
|
})
|
||
|
if err != nil {
|
||
|
applogger.Error("使用Kms解密报错:%v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 输出解密后的明文
|
||
|
decryptedText := decryptResult.Plaintext
|
||
|
applogger.Debug("输出解密后的明文:%v", string(decryptedText))
|
||
|
}
|
||
|
|
||
|
func TestNewKms(t *testing.T) {
|
||
|
type args struct {
|
||
|
a *conf.Aws
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
args args
|
||
|
want *KmsClient
|
||
|
wantErr bool
|
||
|
}{
|
||
|
// TODO: Add test cases.
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
got, err := NewKms(tt.args.a)
|
||
|
if (err != nil) != tt.wantErr {
|
||
|
t.Errorf("NewKms() error = %v, wantErr %v", err, tt.wantErr)
|
||
|
return
|
||
|
}
|
||
|
if !reflect.DeepEqual(got, tt.want) {
|
||
|
t.Errorf("NewKms() got = %v, want %v", got, tt.want)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|