forked from crewjam/go-xmlsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmldsig.go
56 lines (48 loc) · 1.06 KB
/
xmldsig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/crewjam/go-xmlsec"
)
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")
flag.Parse()
if !*doVerify && !*doSign {
fmt.Println("you must specify -v to verify or -s to sign")
os.Exit(1)
}
if *keyPath == "" {
fmt.Println("you must specify a key file")
os.Exit(1)
}
key, err := ioutil.ReadFile(*keyPath)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
buf, err := ioutil.ReadAll(os.Stdin)
if *doSign {
signedBuf, err := xmlsec.Sign(key, buf, xmlsec.SignatureOptions{})
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
os.Stdout.Write(signedBuf)
}
if *doVerify {
err := xmlsec.Verify(key, buf, xmlsec.SignatureOptions{})
if err == xmlsec.ErrVerificationFailed {
fmt.Println("signature is not correct")
os.Exit(1)
}
if err != nil {
fmt.Printf("error: %s\n", err)
os.Exit(1)
}
fmt.Println("signature is correct")
}
}