Last active
June 13, 2017 00:13
-
-
Save athomason/9e3653fc232bd6cdc5048c3a4cbc9321 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: go run decrypt_pgp.go --keyfile private.key.asc file1.gz.pgp file.gz.pgp ... | |
package main | |
import ( | |
"flag" | |
"io" | |
"log" | |
"os" | |
"strings" | |
"golang.org/x/crypto/openpgp" | |
) | |
func main() { | |
keyfilename := flag.String("keyfile", "", "file containing ascii-armored private key") | |
flag.Parse() | |
if *keyfilename == "" { | |
log.Fatal("--keyfile is required") | |
} | |
keyfile, err := os.Open(*keyfilename) | |
if err != nil { | |
log.Fatal(err) | |
} | |
entities, err := openpgp.ReadArmoredKeyRing(keyfile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
keyfile.Close() | |
for _, infile := range flag.Args() { | |
outfile := strings.TrimSuffix(infile, ".pgp") | |
if outfile == infile { | |
log.Printf("%s isn't a .pgp file", infile) | |
continue | |
} | |
in, err := os.Open(infile) | |
if err != nil { | |
log.Printf("couldn't read %s: %s", infile, err) | |
continue | |
} | |
message, err := openpgp.ReadMessage(in, entities, nil, nil) | |
if err != nil { | |
log.Printf("couldn't decrypt %s: %s", infile, err) | |
continue | |
} | |
out, err := os.Create(outfile) | |
if err != nil { | |
log.Printf("couldn't create %s: %s", outfile, err) | |
continue | |
} | |
if _, err = io.Copy(out, message.UnverifiedBody); err != nil { | |
log.Printf("couldn't decrypt to %s: %s", outfile, err) | |
} | |
if err = out.Close(); err != nil { | |
log.Printf("couldn't decrypt to %s: %s", outfile, err) | |
} | |
in.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment