-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcrypt_test.go
More file actions
79 lines (66 loc) · 1.45 KB
/
Copy pathcrypt_test.go
File metadata and controls
79 lines (66 loc) · 1.45 KB
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
package securehttp
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"net/http"
"testing"
)
type dummy struct {
buf []byte
}
func (d *dummy) Write(b []byte) (int, error) {
d.buf = b
return len(b), nil
}
func (d *dummy) Header() http.Header {
return nil
}
func (d *dummy) WriteHeader(int) {}
func (d *dummy) Read(b []byte) (int, error) {
copy(b, d.buf)
return len(d.buf), nil
}
func (d *dummy) Close() error {
return nil
}
func TestCrypt(t *testing.T) {
testCrypt(t, TypeNocrypt, "nokey")
private, _ := rsa.GenerateKey(rand.Reader, 2048)
key := string(x509.MarshalPKCS1PrivateKey(private))
testCrypt(t, TypeRSA, key)
private, _ = rsa.GenerateKey(rand.Reader, 1024)
key = string(x509.MarshalPKCS1PrivateKey(private))
testCrypt(t, TypeRSA, key)
private, _ = rsa.GenerateKey(rand.Reader, 512)
key = string(x509.MarshalPKCS1PrivateKey(private))
testCrypt(t, TypeRSA, key)
}
func testCrypt(t *testing.T, typ, privateKey string) {
d, e := NewDecryptor(typ, privateKey)
if e != nil {
t.Fatal(e)
}
w := &dummy{}
w2, e := NewEncryptedWriter(d.Type(), d.PublicKey(), d.MessageSize(), w)
if e != nil {
t.Fatal(e)
}
input := []byte{97, 98}
w2.Write(input)
w2.(http.Flusher).Flush()
r := d.NewDecryptedReader(w)
b := make([]byte, 5000)
length, e := r.Read(b)
if e != nil {
t.Fatal(e)
}
if length != len(input) {
t.Fatal("read length mismatch")
}
for i, c := range input {
if b[i] != c {
t.Fatal("read content mismatch")
}
}
}