aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-10-02 23:19:38 +0200
committerPřemysl Janouch <p@janouch.name>2018-10-04 01:03:45 +0200
commit43ca0e5035b6951297715a211b73d8db5f751f15 (patch)
tree12b9b653716d896ea690a3daaab036e69308b28f /cmd
parentad239714b0f9fb806abfbb0fbe4420e7304cb09f (diff)
downloadpdf-simple-sign-43ca0e5035b6951297715a211b73d8db5f751f15.tar.gz
pdf-simple-sign-43ca0e5035b6951297715a211b73d8db5f751f15.tar.xz
pdf-simple-sign-43ca0e5035b6951297715a211b73d8db5f751f15.zip
Add a Go port
It should be roughly at feature parity.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/pdf-simple-sign/main.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/cmd/pdf-simple-sign/main.go b/cmd/pdf-simple-sign/main.go
new file mode 100644
index 0000000..11aadaa
--- /dev/null
+++ b/cmd/pdf-simple-sign/main.go
@@ -0,0 +1,72 @@
+//
+// Copyright (c) 2018, Přemysl Janouch <p@janouch.name>
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+//
+
+// pdf-simple-sign is a simple PDF signer.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "janouch.name/pdf-simple-sign/pdf"
+ "os"
+)
+
+// #include <unistd.h>
+import "C"
+
+func isatty(fd uintptr) bool { return C.isatty(C.int(fd)) != 0 }
+
+func die(status int, format string, args ...interface{}) {
+ msg := fmt.Sprintf(format+"\n", args...)
+ if isatty(os.Stderr.Fd()) {
+ msg = "\x1b[0;31m" + msg + "\x1b[m"
+ }
+ os.Stderr.WriteString(msg)
+ os.Exit(status)
+}
+
+func usage() {
+ die(1, "Usage: %s [-h] INPUT-FILENAME OUTPUT-FILENAME "+
+ "PKCS12-PATH PKCS12-PASS", os.Args[0])
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ if flag.NArg() != 4 {
+ usage()
+ }
+
+ inputPath, outputPath := flag.Arg(0), flag.Arg(1)
+ pdfDocument, err := ioutil.ReadFile(inputPath)
+ if err != nil {
+ die(1, "%s", err)
+ }
+ p12, err := ioutil.ReadFile(flag.Arg(2))
+ if err != nil {
+ die(2, "%s", err)
+ }
+ key, certs, err := pdf.PKCS12Parse(p12, flag.Arg(3))
+ if err != nil {
+ die(3, "%s", err)
+ }
+ if pdfDocument, err = pdf.Sign(pdfDocument, key, certs); err != nil {
+ die(2, "error: %s", err)
+ }
+ if err = ioutil.WriteFile(outputPath, pdfDocument, 0666); err != nil {
+ die(3, "%s", err)
+ }
+}