kopia lustrzana https://github.com/reiver/greatape
57 wiersze
1.3 KiB
Go
57 wiersze
1.3 KiB
Go
package utility
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// GenerateRSAKeyPair generates an RSA keypair of 2048 bit size using the
|
|
// random source random (for example, crypto/rand.Reader) and returns the
|
|
// private and public keys
|
|
func GenerateRSAKeyPair() (string, string, error) {
|
|
privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
return "", "", errors.New("Cannot generate RSA key\n")
|
|
}
|
|
|
|
publickey := &privatekey.PublicKey
|
|
|
|
var privateKeyBytes []byte = x509.MarshalPKCS1PrivateKey(privatekey)
|
|
privateKeyBlock := &pem.Block{
|
|
Type: "RSA PRIVATE KEY",
|
|
Bytes: privateKeyBytes,
|
|
}
|
|
|
|
privatePem := bytes.NewBufferString("")
|
|
err = pem.Encode(privatePem, privateKeyBlock)
|
|
if err != nil {
|
|
fmt.Printf("error when encode private pem: %s \n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publickey)
|
|
if err != nil {
|
|
return "", "", fmt.Errorf("error when dumping publickey: %s \n", err)
|
|
}
|
|
|
|
publicKeyBlock := &pem.Block{
|
|
Type: "PUBLIC KEY",
|
|
Bytes: publicKeyBytes,
|
|
}
|
|
|
|
publicPem := bytes.NewBufferString("")
|
|
err = pem.Encode(publicPem, publicKeyBlock)
|
|
if err != nil {
|
|
fmt.Printf("error when encode public pem: %s \n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
return privatePem.String(), publicPem.String(), nil
|
|
}
|