-
Notifications
You must be signed in to change notification settings - Fork 4
/
encryption_test.go
74 lines (68 loc) · 1.79 KB
/
encryption_test.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
package model_test
import (
"bytes"
"crypto/rc4"
"os"
"strings"
"testing"
mo "github.com/benoitkugler/pdf/model"
"github.com/benoitkugler/pdf/reader/file"
)
func TestOverlap(t *testing.T) {
rc, _ := rc4.NewCipher([]byte("medlùl"))
in := []byte("ldsqdlqsùdl")
out := make([]byte, len(in))
rc.XORKeyStream(out, in)
rc, _ = rc4.NewCipher([]byte("medlùl"))
rc.XORKeyStream(in, in)
if !bytes.Equal(out, in) {
t.Errorf("expected same output, got %v and %v", out, in)
}
}
func TestRC4Basic(t *testing.T) {
var doc mo.Document
doc.Catalog.Pages.Kids = []mo.PageNode{&mo.PageObject{Contents: []mo.ContentStream{
{Stream: mo.Stream{Content: []byte(strings.Repeat("dlmskd", 10))}},
}}}
up, op := "dlà&#mks", "elmzk89.ek"
for _, v := range [...]mo.EncryptionAlgorithm{mo.EaRC440, mo.EaRC4Ext} {
for _, p := range [...]mo.UserPermissions{
mo.PermissionPrint,
mo.PermissionModify,
mo.PermissionCopy,
mo.PermissionAdd,
mo.PermissionFill,
mo.PermissionExtract,
mo.PermissionAssemble,
mo.PermissionPrintDigital,
} {
enc := mo.Encrypt{V: v, P: p}
enc = doc.UseStandardEncryptionHandler(enc, op, up, true)
f, err := os.Create("test/rc4.pdf")
if err != nil {
t.Fatal(err)
}
err = doc.Write(f, &enc)
if err != nil {
t.Error(err)
}
f.Close()
_, err = file.ReadFile("test/rc4.pdf", &file.Configuration{Password: up})
if err != nil {
t.Error(err)
}
_, err = file.ReadFile("test/rc4.pdf", &file.Configuration{Password: op})
if err != nil {
t.Error(err)
}
_, err = file.ReadFile("test/rc4.pdf", &file.Configuration{Password: op + "4"})
if err == nil {
t.Errorf("expected error")
}
_, err = file.ReadFile("test/rc4.pdf", &file.Configuration{Password: up + "4"})
if err == nil {
t.Errorf("expected error")
}
}
}
}