SavePrivatePEM saves a private PEM key into a file.
(fileName string, key *rsa.PrivateKey)
| 97 | |
| 98 | // SavePrivatePEM saves a private PEM key into a file. |
| 99 | func SavePrivatePEM(fileName string, key *rsa.PrivateKey) error { |
| 100 | if key == nil { |
| 101 | return errors.New("cannot save nil key") |
| 102 | } |
| 103 | |
| 104 | err := key.Validate() |
| 105 | if err != nil { |
| 106 | return fmt.Errorf("cannot save invalid key: %v", err) |
| 107 | } |
| 108 | |
| 109 | outFile, err := os.Create(fileName) |
| 110 | if err != nil { |
| 111 | return fmt.Errorf("unable to create key file: %v", err) |
| 112 | } |
| 113 | |
| 114 | defer outFile.Close() |
| 115 | |
| 116 | privateKey := &pem.Block{ |
| 117 | Type: "RSA PRIVATE KEY", |
| 118 | Bytes: x509.MarshalPKCS1PrivateKey(key), |
| 119 | } |
| 120 | |
| 121 | err = pem.Encode(outFile, privateKey) |
| 122 | if err != nil { |
| 123 | return fmt.Errorf("error writing key to file: %v", err) |
| 124 | } |
| 125 | |
| 126 | return nil |
| 127 | } |
no test coverage detected