forked from MarinX/agemobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt_test.go
140 lines (123 loc) · 3.02 KB
/
decrypt_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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package agemobile
import (
b64 "encoding/base64"
"fmt"
"io"
"os"
"testing"
"filippo.io/age"
)
func TestDecryptPass(t *testing.T) {
b64key := "YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCBwT29hWWJxSUF6a0FxMnpiSXVJVGxBIDE4CkkrdXpIZk1uQlhqSWxscCtiSDdGOHlZdkNUTkNzUEMyOEx4aXVET2w0ZzQKLS0tIDlYREdmZUJMT2w4Y2Y3M3dsUmJDZlgweTZ6TjExMitUVVdSV1VnM3U5azgKvUblkUHUQXjEyG1kAKL5XmJHKqUuIeClo9b18JN7y4AMo+6qUjZ1IerK6RkRvRoMzG75NKfsJ/YiSFcPC61JW95o73+IXDAvVVLTp44ihm6ESkJeEEOfFVKDstA8jMTSxmxmP32pxkpLav8bAP65mQL/L5JzJ4vJkXUw5Pj/4y/bhtKRiBo86cl8ZAZDJAnHiwCg7JkUm/dxrCCW0Gpqj/lyXaaq+4LjqSo/eAinV5a7vTAWv4jQqE3240hPqVlP7GWqqouS9TMpLmjixhN6sh7kNdYdiUZ/2CPmnJE="
key, _ := b64.StdEncoding.DecodeString(b64key)
enc, err := DecryptPass(
"adjust-sample-fault-museum-grid-uncle-comic-gold-impact-myth",
string(key),
)
if err != nil {
t.Fatal(err)
}
fmt.Println(b64.StdEncoding.EncodeToString([]byte(enc)))
}
func TestDecrypt(t *testing.T) {
txt := "Hello World"
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
out, err := Encrypt(id.Recipient().String(), txt, false)
if err != nil {
t.Fatal(err)
}
dec, err := Decrypt(id.String(), out)
if err != nil {
t.Fatal(err)
}
if dec != txt {
t.Fatalf("Expected %v but got %v\n", txt, dec)
}
}
func TestDecryptArmor(t *testing.T) {
txt := "Hello World"
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
out, err := Encrypt(id.Recipient().String(), txt, true)
if err != nil {
t.Fatal(err)
}
dec, err := Decrypt(id.String(), out)
if err != nil {
t.Fatal(err)
}
if dec != txt {
t.Fatalf("Expected %v but got %v\n", txt, dec)
}
}
func TestDecryptFile(t *testing.T) {
txt := "Hello World"
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
out, err := Encrypt(id.Recipient().String(), txt, false)
if err != nil {
t.Fatal(err)
}
fdin, err := os.CreateTemp("", "age_input_*")
if err != nil {
t.Fatal(err)
}
defer fdin.Close()
io.WriteString(fdin, out)
fdout, err := os.CreateTemp("", "age_*")
if err != nil {
t.Fatal(err)
}
defer fdout.Close()
err = DecryptFile(id.String(), fdin.Name(), fdout.Name())
if err != nil {
t.Fatal(err)
}
buff, err := io.ReadAll(fdout)
if err != nil {
t.Fatal(err)
}
if string(buff) != txt {
t.Fatalf("expected decrypted %v but got %v\n", txt, string(buff))
}
}
func TestDecryptFileArmor(t *testing.T) {
txt := "Hello World"
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
out, err := Encrypt(id.Recipient().String(), txt, true)
if err != nil {
t.Fatal(err)
}
fdin, err := os.CreateTemp("", "age_input_*")
if err != nil {
t.Fatal(err)
}
defer fdin.Close()
io.WriteString(fdin, out)
fdout, err := os.CreateTemp("", "age_*")
if err != nil {
t.Fatal(err)
}
defer fdout.Close()
err = DecryptFile(id.String(), fdin.Name(), fdout.Name())
if err != nil {
t.Fatal(err)
}
buff, err := io.ReadAll(fdout)
if err != nil {
t.Fatal(err)
}
if string(buff) != txt {
t.Fatalf("expected decrypted %v but got %v\n", txt, string(buff))
}
}