Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
coverage.html
coverage.out
coverage.out
/examples/*.pem
/examples/go.mod
/examples/go.sum
/.idea/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
27 changes: 27 additions & 0 deletions examples/example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
XML Security Library example: Simple signature template file for sign1 example.
See https://www.aleksey.com/xmlsec/api/xmlsec-examples-sign-template-file.html#xmlsec-example-sign1-tmpl
-->
<Envelope xmlns="urn:envelope">
<Data>
Hello, World!
</Data>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue></DigestValue>
</Reference>
</SignedInfo>
<SignatureValue/>
<KeyInfo>
<KeyName/>
</KeyInfo>
</Signature>
</Envelope>
16 changes: 15 additions & 1 deletion examples/xmldsig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{})
Expand All @@ -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)
}
}