mirror of
https://github.com/storytold/irsa-manager.git
synced 2026-10-16 00:13:23 +00:00
41 lines
781 B
Go
41 lines
781 B
Go
package selfhosted
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
)
|
|
|
|
type KeyPair struct {
|
|
PublicKey []byte
|
|
PrivateKey []byte
|
|
}
|
|
|
|
func createKeyPair() (*KeyPair, error) {
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// convert private key to PEM
|
|
privPem := pem.Block{
|
|
Type: "RSA PRIVATE KEY",
|
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
|
}
|
|
privPemBytes := pem.EncodeToMemory(&privPem)
|
|
|
|
// convert public key to PKIX, ASN.1 DER
|
|
pubASN1, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pubPem := pem.Block{
|
|
Type: "PUBLIC KEY",
|
|
Bytes: pubASN1,
|
|
}
|
|
pubPemBytes := pem.EncodeToMemory(&pubPem)
|
|
return &KeyPair{pubPemBytes, privPemBytes}, nil
|
|
}
|