mailout/pkg/crypto/key.go

31 lines
638 B
Go

package crypto
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
)
func GenerateKeyPair(bytes int) (string, string, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, bytes)
if err != nil {
return "", "", err
}
err = privateKey.Validate()
if err != nil {
return "", "", err
}
privBlock := pem.Block{
Type: "PRIVATE KEY",
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
privateKeyPEM := pem.EncodeToMemory(&privBlock)
return string(privateKeyPEM), string(base64.StdEncoding.EncodeToString(x509.MarshalPKCS1PublicKey(&privateKey.PublicKey))), nil
}