From abf855d58dff6569d8d13121369b950ac780ee73 Mon Sep 17 00:00:00 2001 From: Franco Lombardo Date: Sat, 6 Mar 2021 11:32:52 +0100 Subject: [PATCH] Added instructions on how to run the example --- .gitignore | 6 +++++- .idea/.gitignore | 8 ++++++++ README.md | 8 ++++++++ examples/example.xml | 27 +++++++++++++++++++++++++++ examples/xmldsig.go | 16 +++++++++++++++- 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 examples/example.xml diff --git a/.gitignore b/.gitignore index 6b72022..17c0e14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ coverage.html -coverage.out \ No newline at end of file +coverage.out +/examples/*.pem +/examples/go.mod +/examples/go.sum +/.idea/ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/README.md b/README.md index aa50989..bd3766a 100644 --- a/README.md +++ b/README.md @@ -92,4 +92,12 @@ go build -tags static -ldflags '-s -extldflags "-static"' -o /bin/xmldsig-static Running `ldd` on the output should produce `not a dynamic executable`. +# Running the example +After cloning this repository, change your current directory to the `examples` one, then run: +``` +openssl req -x509 -nodes -newkey rsa:4096 -sha512 -keyout key.pem -out crt.pem -days 1095 +go mod init example +go mod tidy +go run xmldsig.go -s -k key.pem -x example.xml +``` diff --git a/examples/example.xml b/examples/example.xml new file mode 100644 index 0000000..21a5851 --- /dev/null +++ b/examples/example.xml @@ -0,0 +1,27 @@ + + + + + Hello, World! + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/xmldsig.go b/examples/xmldsig.go index 31608ec..d361fdd 100644 --- a/examples/xmldsig.go +++ b/examples/xmldsig.go @@ -13,6 +13,7 @@ func main() { doVerify := flag.Bool("v", false, "verify the document") doSign := flag.Bool("s", false, "sign the document") keyPath := flag.String("k", "", "the path to the key") + xmlFile := flag.String("x", "", "the path to the xml file") flag.Parse() if !*doVerify && !*doSign { @@ -30,7 +31,12 @@ func main() { os.Exit(1) } - buf, err := ioutil.ReadAll(os.Stdin) + buf, err := readXml(xmlFile) + + if err != nil { + fmt.Printf("%s\n", err) + os.Exit(1) + } if *doSign { signedBuf, err := xmlsec.Sign(key, buf, xmlsec.SignatureOptions{}) @@ -54,3 +60,11 @@ func main() { fmt.Println("signature is correct") } } + +func readXml(xmlFileName *string) ([]byte, error) { + if *xmlFileName == "" { + return ioutil.ReadAll(os.Stdin) + } else { + return ioutil.ReadFile(*xmlFileName) + } +}