73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package test
|
|
|
|
import (
|
|
"acme-mana/src/crypto"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"log"
|
|
"testing"
|
|
)
|
|
|
|
func TestGenRsa(t *testing.T) {
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
pubKey := base64.StdEncoding.EncodeToString(publicKeyBytes)
|
|
key := x509.MarshalPKCS1PrivateKey(privateKey)
|
|
priKey := base64.StdEncoding.EncodeToString(key)
|
|
fmt.Println(pubKey)
|
|
fmt.Println()
|
|
fmt.Println(priKey)
|
|
}
|
|
|
|
func TestAesEncrypt(t *testing.T) {
|
|
key := []byte("12345678901234561234567890123456")
|
|
encryptd := crypto.EncryptAES(key, []byte("hello"))
|
|
fmt.Println(base64.StdEncoding.EncodeToString(encryptd))
|
|
content := crypto.DecryptAES(key, encryptd)
|
|
fmt.Println(string(content))
|
|
}
|
|
|
|
func TestRSA(t *testing.T) {
|
|
priKey, pubKey, err := crypto.GenRSA()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println("---- pri key ---")
|
|
fmt.Println(priKey)
|
|
fmt.Println("---- pub key ---")
|
|
fmt.Println(pubKey)
|
|
fmt.Println("---- encrypt ---")
|
|
|
|
content := "hello"
|
|
encrypted, err := crypto.EncryptRSABase64(pubKey, []byte(content))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(base64.StdEncoding.EncodeToString(encrypted))
|
|
fmt.Println("---- decrypt ---")
|
|
decryptd, err := crypto.DecryptRSABase64(priKey, encrypted)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(string(decryptd))
|
|
|
|
}
|
|
|
|
func TestAES(t *testing.T) {
|
|
content := "123456"
|
|
encryptd := crypto.EncryptAES([]byte("12345678901234561234567890123456"), []byte(content))
|
|
fmt.Println(base64.StdEncoding.EncodeToString(encryptd))
|
|
plain := crypto.DecryptAES([]byte("12345678901234561234567890123456"), encryptd)
|
|
fmt.Println(string(plain))
|
|
|
|
}
|