-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption_obj.go
58 lines (48 loc) · 1.29 KB
/
encryption_obj.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
package gopdf
import (
"bytes"
"fmt"
"strings"
)
//EncryptionObj encryption object res
type EncryptionObj struct {
buffer bytes.Buffer
uValue []byte //U entry in pdf document
oValue []byte //O entry in pdf document
pValue int //P entry in pdf document
}
func (e *EncryptionObj) init(func() *GoPdf) {
}
func (e *EncryptionObj) getType() string {
return "Encryption"
}
func (e *EncryptionObj) getObjBuff() *bytes.Buffer {
return &e.buffer
}
func (e *EncryptionObj) build(objID int) error {
e.buffer.WriteString("<<\n")
e.buffer.WriteString("/Filter /Standard\n")
e.buffer.WriteString("/V 1\n")
e.buffer.WriteString("/R 2\n")
e.buffer.WriteString(fmt.Sprintf("/O (%s)\n", e.escape(e.oValue)))
e.buffer.WriteString(fmt.Sprintf("/U (%s)\n", e.escape(e.uValue)))
e.buffer.WriteString(fmt.Sprintf("/P %d\n", e.pValue))
e.buffer.WriteString(">>\n")
return nil
}
func (e *EncryptionObj) escape(b []byte) string {
s := string(b)
s = strings.Replace(s, "\\", "\\\\", -1)
s = strings.Replace(s, "(", "\\(", -1)
s = strings.Replace(s, ")", "\\)", -1)
s = strings.Replace(s, "\r", "\\r", -1)
return s
}
//GetObjBuff get buffer
func (e *EncryptionObj) GetObjBuff() *bytes.Buffer {
return e.getObjBuff()
}
//Build build buffer
func (e *EncryptionObj) Build(objID int) error {
return e.build(objID)
}