Add pem2bin, a utility for dumping the contents of PEM blocks.

This commit is contained in:
Kyle Isom 2016-01-14 17:44:30 -08:00
parent 8fd2e76939
commit f5a9762b28
3 changed files with 48 additions and 0 deletions

View File

@ -18,6 +18,7 @@ Contents:
certificate request.
fragment/ Print a fragment of a file.
jlp/ JSON linter/prettifier.
pem2bin/ Dump the binary body of a PEM-encoded block.
pembody/ Print the body of a PEM certificate.
showimp/ List the external (e.g. non-stdlib and outside the
current working directory) imports for a Go file.

8
cmd/pem2bin/README Normal file
View File

@ -0,0 +1,8 @@
pem2bin
This is a utility that strips the PEM-encoding from a file and dumps
the binary body to disk. The resulting filename is the original pathname
with ".bin" appended. Note that only the first PEM block in a file will
be decoded.
Usage: pem2bin paths*

39
cmd/pem2bin/main.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"os"
)
var ext = ".bin"
func stripPEM(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
p, rest := pem.Decode(data)
if len(rest) != 0 {
fmt.Fprintf(os.Stderr, "[WARNING] extra data in PEM file\n")
fmt.Fprintf(os.Stderr, " (only the first object will be decoded)\n")
}
return ioutil.WriteFile(path+ext, p.Bytes, 0644)
}
func main() {
flag.Parse()
for _, path := range flag.Args() {
err := stripPEM(path)
if err != nil {
fmt.Fprintf(os.Stderr, "processing %s failed: %v\n", path, err)
} else {
fmt.Println(path, "->", path+ext)
}
}
}