From 292fa7ee4fcab5da4d976ffb8dd15fbb67dfd214 Mon Sep 17 00:00:00 2001 From: adnano Date: Tue, 13 Oct 2020 16:50:59 -0400 Subject: Remove WriteX509KeyPair function --- examples/cert.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'examples/cert.go') diff --git a/examples/cert.go b/examples/cert.go index d6ab53b..7d3597d 100644 --- a/examples/cert.go +++ b/examples/cert.go @@ -4,6 +4,7 @@ package main import ( "log" + "os" "time" "git.sr.ht/~adnano/gmi" @@ -11,14 +12,38 @@ import ( func main() { host := "localhost" - - duration := 2 * time.Minute + duration := 365 * 24 * time.Hour crt, key, err := gmi.NewRawCertificate(host, duration) if err != nil { log.Fatal(err) } - if err := gmi.WriteX509KeyPair(host, crt, key); err != nil { + 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 +} -- cgit v1.2.3