1
0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-09-20 07:21:53 +02:00
go-gemini/examples/cert.go

50 lines
972 B
Go
Raw Normal View History

2020-10-12 22:34:52 +02:00
// +build ignore
2020-09-27 19:50:48 +02:00
package main
import (
"log"
2020-10-13 22:50:59 +02:00
"os"
2020-09-28 05:49:41 +02:00
"time"
2020-09-27 19:50:48 +02:00
2020-09-29 00:19:59 +02:00
"git.sr.ht/~adnano/gmi"
2020-09-27 19:50:48 +02:00
)
func main() {
host := "localhost"
2020-10-13 22:50:59 +02:00
duration := 365 * 24 * time.Hour
2020-09-28 05:49:41 +02:00
crt, key, err := gmi.NewRawCertificate(host, duration)
2020-09-27 19:50:48 +02:00
if err != nil {
log.Fatal(err)
}
2020-10-13 22:50:59 +02:00
if err := writeX509KeyPair(host, crt, key); err != nil {
2020-09-27 19:50:48 +02:00
log.Fatal(err)
}
}
2020-10-13 22:50:59 +02:00
// 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
}