forked from moov-io/signedxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canonicalization.go
96 lines (81 loc) · 2.49 KB
/
canonicalization.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package signedxml
import (
"github.com/beevik/etree"
dsig "github.com/russellhaering/goxmldsig"
)
type c14N10RecCanonicalizer struct {
WithComments bool
}
func (c *c14N10RecCanonicalizer) ProcessElement(inputXML *etree.Element, transformXML string) (outputXML string, err error) {
transformedXML, err := c.processElement(inputXML, transformXML)
if err != nil {
return "", err
}
return transformedXML, nil
}
func (c *c14N10RecCanonicalizer) ProcessDocument(doc *etree.Document, transformXML string) (outputXML string, err error) {
transformedXML, err := c.processElement(doc.Root(), transformXML)
if err != nil {
return "", err
}
return transformedXML, nil
}
func (c c14N10RecCanonicalizer) Process(inputXML string, transformXML string) (outputXML string, err error) {
doc := etree.NewDocument()
err = doc.ReadFromString(inputXML)
if err != nil {
return "", err
}
return c.ProcessDocument(doc, transformXML)
}
func (c *c14N10RecCanonicalizer) processElement(inputXML *etree.Element, transformXML string) (outputXML string, err error) {
var canon dsig.Canonicalizer
if c.WithComments {
canon = dsig.MakeC14N10WithCommentsCanonicalizer()
} else {
canon = dsig.MakeC14N10RecCanonicalizer()
}
out, err := canon.Canonicalize(inputXML)
if err != nil {
return "", err
}
return string(out), nil
}
type c14N11Canonicalizer struct {
WithComments bool
}
func (c *c14N11Canonicalizer) ProcessElement(inputXML *etree.Element, transformXML string) (outputXML string, err error) {
transformedXML, err := c.processElement(inputXML, transformXML)
if err != nil {
return "", err
}
return transformedXML, nil
}
func (c *c14N11Canonicalizer) ProcessDocument(doc *etree.Document, transformXML string) (outputXML string, err error) {
transformedXML, err := c.processElement(doc.Root(), transformXML)
if err != nil {
return "", err
}
return transformedXML, nil
}
func (c c14N11Canonicalizer) Process(inputXML string, transformXML string) (outputXML string, err error) {
doc := etree.NewDocument()
err = doc.ReadFromString(inputXML)
if err != nil {
return "", err
}
return c.ProcessDocument(doc, transformXML)
}
func (c *c14N11Canonicalizer) processElement(inputXML *etree.Element, transformXML string) (outputXML string, err error) {
var canon dsig.Canonicalizer
if c.WithComments {
canon = dsig.MakeC14N11WithCommentsCanonicalizer()
} else {
canon = dsig.MakeC14N11Canonicalizer()
}
out, err := canon.Canonicalize(inputXML)
if err != nil {
return "", err
}
return string(out), nil
}