-
Notifications
You must be signed in to change notification settings - Fork 4
/
signatures.go
81 lines (76 loc) · 1.92 KB
/
signatures.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
package ofd
import (
"archive/zip"
"encoding/xml"
"fmt"
"strings"
)
type SignaturesXml struct {
XMLName xml.Name `xml:"Signatures"`
Text string `xml:",chardata"`
Ofd string `xml:"ofd,attr"`
MaxSignId struct {
Text string `xml:",chardata"`
} `xml:"MaxSignId"`
Signature []struct {
Text string `xml:",chardata"`
ID string `xml:"ID,attr"`
Type string `xml:"Type,attr"`
BaseLoc string `xml:"BaseLoc,attr"`
} `xml:"Signature"`
}
type Signatures struct {
SignaturesXml
pwd string
rc *zip.ReadCloser
Validator
}
func (signatures Signatures) GetFileContent(path string) ([]byte, error) {
return LoadZipFileContent(signatures.rc, path)
}
func (signatures Signatures) GetSignatureById(signId string) (*Signature, error) {
if strings.EqualFold(signId, "") {
return nil, fmt.Errorf("signId is empty")
}
for _, sign := range signatures.Signature {
if !strings.EqualFold(signId, sign.ID) {
continue
}
path := sign.BaseLoc
pos := strings.LastIndexByte(path, '/')
pwd := path[0:pos]
content, err := signatures.GetFileContent(path)
if err != nil {
if path[0] == '/' {
path = signatures.pwd + path
} else {
path = signatures.pwd + "/" + path
}
pos = strings.LastIndexByte(path, '/')
pwd = path[0:pos]
content, err = signatures.GetFileContent(path)
if err != nil {
return nil, err
}
}
var signature Signature
if err := xml.Unmarshal(content, &signature); err != nil {
return nil, err
} else {
signature.pwd = pwd
signature.rc = signatures.rc
signature.Validator = signatures.Validator
signature.Content = content
switch sign.Type {
case "Seal":
signature.Category = SEAL
case "Sign":
signature.Category = SIGN
default:
signature.Category = SEAL
}
}
return &signature, nil
}
return nil, fmt.Errorf("signId [%v] not found", signId)
}