MCPcopy
hub / github.com/apptainer/apptainer / SavePrivatePEM

Function SavePrivatePEM

pkg/util/cryptkey/rsa.go:99–127  ·  view source on GitHub ↗

SavePrivatePEM saves a private PEM key into a file.

(fileName string, key *rsa.PrivateKey)

Source from the content-addressed store, hash-verified

97
98// SavePrivatePEM saves a private PEM key into a file.
99func 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}

Callers 1

GeneratePemFilesFunction · 0.92

Calls 2

CreateMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected