Last active
June 27, 2018 13:59
-
-
Save bunyk/660fa241fd61320f47d27034cd6070de to your computer and use it in GitHub Desktop.
RSA Keypair generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/rand" | |
"crypto/rsa" | |
"crypto/x509" | |
"encoding/pem" | |
"fmt" | |
"log" | |
) | |
func main() { | |
privateKey, err := rsa.GenerateKey(rand.Reader, 1024) | |
if err != nil { | |
log.Fatal(err) | |
} | |
pemdata := pem.EncodeToMemory( | |
&pem.Block{ | |
Type: "RSA PRIVATE KEY", | |
Bytes: x509.MarshalPKCS1PrivateKey(privateKey), | |
}, | |
) | |
fmt.Println(string(pemdata)) | |
pemdata = pem.EncodeToMemory( | |
&pem.Block{ | |
Type: "PUBLIC KEY", | |
Bytes: x509.MarshalPKCS1PublicKey(&privateKey.PublicKey), | |
}, | |
) | |
fmt.Println(string(pemdata)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment