| 1 | package receiver_test |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "crypto/rsa" |
| 6 | "crypto/x509" |
| 7 | "encoding/pem" |
| 8 | "os" |
| 9 | ) |
| 10 | |
| 11 | func genPrivateKey() (*rsa.PrivateKey, error) { |
| 12 | const bits = 2048 |
| 13 | priv, err := rsa.GenerateKey(rand.Reader, bits) |
| 14 | if err != nil { |
| 15 | return nil, err |
| 16 | } |
| 17 | if err := priv.Validate(); err != nil { |
| 18 | return nil, err |
| 19 | } |
| 20 | return priv, nil |
| 21 | } |
| 22 | |
| 23 | func asPEM(priv *rsa.PrivateKey) []byte { |
| 24 | return pem.EncodeToMemory(&pem.Block{ |
| 25 | Type: "RSA PRIVATE KEY", |
| 26 | Headers: nil, |
| 27 | Bytes: x509.MarshalPKCS1PrivateKey(priv), |
| 28 | }) |
| 29 | } |
| 30 | |
| 31 | func genKey(privKeyPath string) error { |
| 32 | priv, err := genPrivateKey() |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if err := os.WriteFile(privKeyPath, asPEM(priv), 0600); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | return nil |
| 40 | } |