package main // pemdecode ver. 1.0 // coded by Kenar import ( . "fmt" "os" "encoding/pem" "strconv" ) var usage = `usage: pemdecode cert.pem [n] n is 1,2,... n: certificate number in cert.pem to be decoded if n is not given, all certificates will be decoded` func Error(s interface{}){ Fprintln(os.Stderr,s) os.Exit(1) } func check(err error) { if err != nil { Error(err) } } const ( max = 10 ) func main() { var pb [max]*pem.Block var n int var err error var bs []byte args := os.Args[1:] if len(args) != 1 && len(args) != 2{ Error(usage) } bs,err = os.ReadFile(args[0]) check(err) for k:=0; len(bs) > 0 ; k++ { pb[k],bs = pem.Decode(bs) } if len(args) == 2 { n,err = strconv.Atoi(args[1]) check(err) if n < 1 || n >= max || pb[n-1] == nil { Error(usage) } Printf("%s", pb[n-1].Bytes) return } for k:=0; pb[k] != nil; k++ { if k != 0 { Println() } Printf("%s", pb[k].Bytes) } }