1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-06-09 09:16:07 +02:00
go-gemini/examples/cert.go
2020-10-13 16:50:59 -04:00

50 lines
972 B
Go

// +build ignore
package main
import (
"log"
"os"
"time"
"git.sr.ht/~adnano/gmi"
)
func main() {
host := "localhost"
duration := 365 * 24 * time.Hour
crt, key, err := gmi.NewRawCertificate(host, duration)
if err != nil {
log.Fatal(err)
}
if err := writeX509KeyPair(host, crt, key); err != nil {
log.Fatal(err)
}
}
// writeX509KeyPair writes the provided certificate and private key
// to path.crt and path.key respectively.
func writeX509KeyPair(path string, crt, key []byte) error {
// Write the certificate
crtPath := path + ".crt"
crtOut, err := os.OpenFile(crtPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
if _, err := crtOut.Write(crt); err != nil {
return err
}
// Write the private key
keyPath := path + ".key"
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
if _, err := keyOut.Write(key); err != nil {
return err
}
return nil
}