-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmem_signer.go
123 lines (96 loc) · 2.37 KB
/
mem_signer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package passkit
import (
"archive/zip"
"bytes"
"crypto/sha1"
"encoding/json"
"fmt"
)
type memorySigner struct {
}
func NewMemoryBasedSigner() Signer {
return &memorySigner{}
}
func (m *memorySigner) CreateSignedAndZippedPassArchive(p *Pass, t PassTemplate, i *SigningInformation) ([]byte, error) {
return m.CreateSignedAndZippedPersonalizedPassArchive(p, nil, t, i)
}
func (m *memorySigner) CreateSignedAndZippedPersonalizedPassArchive(p *Pass, pz *Personalization, t PassTemplate, i *SigningInformation) ([]byte, error) {
originalFiles, err := t.GetAllFiles()
if err != nil {
return nil, err
}
files := m.makeFilesCopy(originalFiles)
if !p.IsValid() {
return nil, fmt.Errorf("%v", p.GetValidationErrors())
}
pb, err := p.toJSON()
if err != nil {
return nil, err
}
files[passJsonFileName] = pb
if pz != nil {
if !pz.IsValid() {
return nil, fmt.Errorf("%v", pz.GetValidationErrors())
}
pzb, err := pz.toJSON()
if err != nil {
return nil, err
}
files[personalizationJsonFileName] = pzb
}
msftHash, err := m.hashFiles(files)
if err != nil {
return nil, err
}
mfst, err := json.Marshal(msftHash)
if err != nil {
return nil, err
}
files[manifestJsonFileName] = mfst
signedMfst, err := signManifestFile(mfst, i)
if err != nil {
return nil, err
}
files[signatureFileName] = signedMfst
z, err := m.createZipFile(files)
if err != nil {
return nil, err
}
return z, nil
}
func (m *memorySigner) SignManifestFile(manifestJson []byte, i *SigningInformation) ([]byte, error) {
return signManifestFile(manifestJson, i)
}
func (m *memorySigner) hashFiles(files map[string][]byte) (map[string]string, error) {
ret := make(map[string]string)
for name, data := range files {
hash := sha1.Sum(data)
ret[name] = fmt.Sprintf("%x", hash)
}
return ret, nil
}
func (m *memorySigner) createZipFile(files map[string][]byte) ([]byte, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for name, data := range files {
f, err := w.Create(name)
if err != nil {
return nil, err
}
_, err = f.Write(data)
if err != nil {
return nil, err
}
}
if err := w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (m *memorySigner) makeFilesCopy(files map[string][]byte) map[string][]byte {
filesCopy := make(map[string][]byte, len(files))
for k := range files {
filesCopy[k] = files[k]
}
return filesCopy
}