Add pem2bin, a utility for dumping the contents of PEM blocks.
This commit is contained in:
parent
8fd2e76939
commit
f5a9762b28
|
@ -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.
|
||||
|
|
|
@ -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*
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue