32 lines
726 B
Go
32 lines
726 B
Go
package test
|
|
|
|
import (
|
|
"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)
|
|
//publicKey := &privateKey.PublicKey
|
|
//publicKeyBytes := x509.MarshalPKCS1PublicKey(publicKey)
|
|
//pubKey := base64.StdEncoding.EncodeToString(publicKeyBytes)
|
|
key := x509.MarshalPKCS1PrivateKey(privateKey)
|
|
priKey := base64.StdEncoding.EncodeToString(key)
|
|
fmt.Println(pubKey)
|
|
fmt.Println()
|
|
fmt.Println(priKey)
|
|
}
|