Created
October 10, 2017 19:18
-
-
Save naren-m/d0ae1e7f2563fba01d1d421dc42171cd to your computer and use it in GitHub Desktop.
generating pem file
This file contains hidden or 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
| // Run the code and will ask for input. once you enter password it will create the pemfile | |
| package main | |
| import ( | |
| "crypto/rand" | |
| "crypto/rsa" | |
| "crypto/x509" | |
| "encoding/pem" | |
| "fmt" | |
| "os" | |
| "reflect" | |
| ) | |
| func main() { | |
| reader := rand.Reader | |
| bitSize := 4096 | |
| key, err := rsa.GenerateKey(reader, bitSize) | |
| checkError(err) | |
| fmt.Println("Private key primes", key.Primes[0].String(), key.Primes[1].String()) | |
| fmt.Println("Private key exponent", key.D.String()) | |
| publicKey := key.PublicKey | |
| fmt.Println("Public key modulus", publicKey.N.String()) | |
| fmt.Println("Public key exponent", publicKey.E) | |
| pemFilePath := "pemfile.pem" | |
| fmt.Println(reflect.TypeOf(key)) | |
| savePEMKey(pemFilePath, key) | |
| } | |
| func savePEMKey(fileName string, key *rsa.PrivateKey) { | |
| outFile, err := os.Create(fileName) | |
| checkError(err) | |
| var privateKey = &pem.Block{Type: "RSA PRIVATE KEY", | |
| Bytes: x509.MarshalPKCS1PrivateKey(key)} | |
| pem.Encode(outFile, privateKey) | |
| outFile.Close() | |
| } | |
| func checkError(err error) { | |
| if err != nil { | |
| fmt.Println("Fatal error ", err.Error()) | |
| os.Exit(1) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment