-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
159 lines (127 loc) · 3.78 KB
/
example_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package age_test
import (
"bytes"
"fmt"
"io"
"filippo.io/age"
yamlage "sylr.dev/yaml/age/v3"
yaml "sylr.dev/yaml/v3"
)
func ExampleWrapper() {
yamlString := `password: !crypto/age |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCB4c3VtbURKYlhNclZORExq
cVdyM1RnIDE4ClJ3ejBxU292WGJpQWtLQ1NXMnN4THk5VWQvLzVzKzBmWTQvOVp5
MTQrak0KLS0tIFI1U1RnZXFDVU5YbGJTU3lpNnBOdEVybDdtQmUrM1VkcHV4OElN
Zm1aZ1kKvhgBDqN8umSS+EmwRwAKj9wNicvbWuynN7W0wxu6apXn57icXGgxiFK0
8zlxcVRSeplPrnuRdOUBgjoNtdUt
-----END AGE ENCRYPTED FILE-----`
rbuf := bytes.NewBufferString(yamlString)
wbuf := bytes.NewBuffer(nil)
node := struct {
Password yamlage.String `yaml:"password"`
}{}
id, err := age.NewScryptIdentity("point-adjust-member-tip-tiger-limb-honey-prefer-copy-issue")
if err != nil {
panic(err)
}
w := yamlage.Wrapper{
Value: &node,
Identities: []age.Identity{id},
}
decoder := yaml.NewDecoder(rbuf)
encoder := yaml.NewEncoder(wbuf)
encoder.SetIndent(2)
err = decoder.Decode(&w)
if err != nil {
panic(err)
}
err = encoder.Encode(&node)
if err != nil {
panic(err)
}
fmt.Printf("%s", wbuf.String())
// Output:
// password: !crypto/age ThisIsMyReallyEncryptedPassword
}
func ExampleWrapper_anonymous() {
yamlString := `
password: !crypto/age:DoubleQuoted |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCB4c3VtbURKYlhNclZORExq
cVdyM1RnIDE4ClJ3ejBxU292WGJpQWtLQ1NXMnN4THk5VWQvLzVzKzBmWTQvOVp5
MTQrak0KLS0tIFI1U1RnZXFDVU5YbGJTU3lpNnBOdEVybDdtQmUrM1VkcHV4OElN
Zm1aZ1kKvhgBDqN8umSS+EmwRwAKj9wNicvbWuynN7W0wxu6apXn57icXGgxiFK0
8zlxcVRSeplPrnuRdOUBgjoNtdUt
-----END AGE ENCRYPTED FILE-----
---
password: !crypto/age:SingleQuoted |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCB4c3VtbURKYlhNclZORExq
cVdyM1RnIDE4ClJ3ejBxU292WGJpQWtLQ1NXMnN4THk5VWQvLzVzKzBmWTQvOVp5
MTQrak0KLS0tIFI1U1RnZXFDVU5YbGJTU3lpNnBOdEVybDdtQmUrM1VkcHV4OElN
Zm1aZ1kKvhgBDqN8umSS+EmwRwAKj9wNicvbWuynN7W0wxu6apXn57icXGgxiFK0
8zlxcVRSeplPrnuRdOUBgjoNtdUt
-----END AGE ENCRYPTED FILE-----
---
password: !crypto/age:DoubleQuoted,NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCB4c3VtbURKYlhNclZORExq
cVdyM1RnIDE4ClJ3ejBxU292WGJpQWtLQ1NXMnN4THk5VWQvLzVzKzBmWTQvOVp5
MTQrak0KLS0tIFI1U1RnZXFDVU5YbGJTU3lpNnBOdEVybDdtQmUrM1VkcHV4OElN
Zm1aZ1kKvhgBDqN8umSS+EmwRwAKj9wNicvbWuynN7W0wxu6apXn57icXGgxiFK0
8zlxcVRSeplPrnuRdOUBgjoNtdUt
-----END AGE ENCRYPTED FILE-----`
rbuf := bytes.NewBufferString(yamlString)
wbuf := bytes.NewBuffer(nil)
id, err := age.NewScryptIdentity("point-adjust-member-tip-tiger-limb-honey-prefer-copy-issue")
if err != nil {
panic(err)
}
node := &yaml.Node{}
w := yamlage.Wrapper{
Value: node,
Identities: []age.Identity{id},
}
decoder := yaml.NewDecoder(rbuf)
encoder := yaml.NewEncoder(wbuf)
encoder.SetIndent(2)
for {
err = decoder.Decode(&w)
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
err = encoder.Encode(&node)
if err != nil {
panic(err)
}
}
fmt.Printf("%s", wbuf.String())
// Output:
// password: !crypto/age:DoubleQuoted "ThisIsMyReallyEncryptedPassword"
// ---
// password: !crypto/age:SingleQuoted 'ThisIsMyReallyEncryptedPassword'
// ---
// password: "ThisIsMyReallyEncryptedPassword"
}
func ExampleString_encode() {
rec, err := age.NewScryptRecipient("point-adjust-member-tip-tiger-limb-honey-prefer-copy-issue")
if err != nil {
panic(err)
}
node := struct {
Password yamlage.String `yaml:"password"`
}{
Password: yamlage.NewString("ThisIsMyReallyEncryptedPassword", []age.Recipient{rec}),
}
buf := bytes.NewBuffer(nil)
encoder := yaml.NewEncoder(buf)
encoder.SetIndent(2)
err = encoder.Encode(&node)
if err != nil {
panic(err)
}
fmt.Printf("%s", buf.String())
}