8
8
"golang.org/x/crypto/chacha20poly1305"
9
9
)
10
10
11
- // EncryptChacha20poly1305 encrypts and authenticates the given message with
11
+ // EncryptByteChacha20poly1305 encrypts and authenticates the given message (bytes) with
12
12
// ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
13
- func EncryptChacha20poly1305 (key []byte , text string ) (ciphertext []byte , nonce []byte , err error ) {
13
+ func EncryptByteChacha20poly1305 (key []byte , input [] byte ) (ciphertext []byte , nonce []byte , err error ) {
14
14
// create a new ChaCha20-Poly1305 AEAD using the given 256-bit key
15
15
aead , err := chacha20poly1305 .New (key )
16
16
if err != nil {
@@ -26,18 +26,20 @@ func EncryptChacha20poly1305(key []byte, text string) (ciphertext []byte, nonce
26
26
return
27
27
}
28
28
29
- // data to be encrypted
30
- data := []byte (text )
31
-
32
29
// encrypt the data
33
- ciphertext = aead .Seal (nil , nonce , data , nil )
34
-
30
+ ciphertext = aead .Seal (nil , nonce , input , nil )
35
31
return
36
32
}
37
33
38
- // DecryptChacha20poly1305 decrypts and authenticates the given message with
34
+ // EncryptChacha20poly1305 encrypts and authenticates the given message (string) with
39
35
// ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
40
- func DecryptChacha20poly1305 (key , nonce , ciphertext []byte ) (text string , err error ) {
36
+ func EncryptChacha20poly1305 (key []byte , text string ) (ciphertext []byte , nonce []byte , err error ) {
37
+ return EncryptByteChacha20poly1305 (key , []byte (text ))
38
+ }
39
+
40
+ // DecryptByteChacha20poly1305 decrypts and authenticates the given ciphertext with
41
+ // ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
42
+ func DecryptByteChacha20poly1305 (key , nonce , ciphertext []byte ) (plaintext []byte , err error ) {
41
43
// create a new ChaCha20-Poly1305 AEAD using the given 256-bit key
42
44
aead , err := chacha20poly1305 .New (key )
43
45
if err != nil {
@@ -46,45 +48,78 @@ func DecryptChacha20poly1305(key, nonce, ciphertext []byte) (text string, err er
46
48
}
47
49
48
50
// decrypt the data
49
- plaintext , err : = aead .Open (nil , nonce , ciphertext , nil )
51
+ plaintext , err = aead .Open (nil , nonce , ciphertext , nil )
50
52
if err != nil {
51
53
err = fmt .Errorf ("error decrypting data: %v" , err )
52
54
return
53
55
}
54
- text = string (plaintext )
55
56
56
57
return
57
58
}
58
59
59
- // EncryptChacha20poly1305WithNonceAppended encrypts and authenticates the given message with
60
+ // DecryptChacha20poly1305 decrypts and authenticates the given ciphertext with
61
+ // ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
62
+ func DecryptChacha20poly1305 (key , nonce , ciphertext []byte ) (text string , err error ) {
63
+ // decrypt the data
64
+ plaintext , err := DecryptByteChacha20poly1305 (key , nonce , ciphertext )
65
+ if err != nil {
66
+ return
67
+ }
68
+
69
+ text = string (plaintext )
70
+ return
71
+ }
72
+
73
+ // EncryptByteChacha20poly1305WithNonceAppended encrypts and authenticates the given message (bytes) with
60
74
// ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
61
75
// It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
62
- func EncryptChacha20poly1305WithNonceAppended (key []byte , text string ) (ciphertext []byte , err error ) {
63
- ciphertext , nonce , err := EncryptChacha20poly1305 (key , text )
76
+ func EncryptByteChacha20poly1305WithNonceAppended (key []byte , input [] byte ) (ciphertext []byte , err error ) {
77
+ ciphertext , nonce , err := EncryptByteChacha20poly1305 (key , input )
64
78
if err != nil {
65
79
return
66
80
}
81
+
67
82
ciphertext = append (nonce , ciphertext ... )
68
83
return
69
84
}
70
85
71
- // DecryptChacha20poly1305WithNonceAppended decrypts and authenticates the given message with
86
+ // EncryptChacha20poly1305WithNonceAppended encrypts and authenticates the given message (string) with
87
+ // ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
88
+ // It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
89
+ func EncryptChacha20poly1305WithNonceAppended (key []byte , text string ) (ciphertext []byte , err error ) {
90
+ return EncryptByteChacha20poly1305WithNonceAppended (key , []byte (text ))
91
+ }
92
+
93
+ // DecryptByteChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with
72
94
// ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
73
95
// It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
74
- func DecryptChacha20poly1305WithNonceAppended (key , ciphertext []byte ) (text string , err error ) {
96
+ func DecryptByteChacha20poly1305WithNonceAppended (key , ciphertext []byte ) (plaintext [] byte , err error ) {
75
97
nonceSize := chacha20poly1305 .NonceSize
76
98
if len (ciphertext ) < nonceSize {
77
99
err = errors .New ("ciphertext is too short" )
78
100
return
79
101
}
102
+
80
103
nonce , ciphertext := ciphertext [:nonceSize ], ciphertext [nonceSize :]
81
- text , err = DecryptChacha20poly1305 (key , nonce , ciphertext )
104
+ return DecryptByteChacha20poly1305 (key , nonce , ciphertext )
105
+ }
106
+
107
+ // DecryptChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with
108
+ // ChaCha20-Poly1305 AEAD using the given 256-bit key and 96-bit nonce.
109
+ // It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
110
+ func DecryptChacha20poly1305WithNonceAppended (key , ciphertext []byte ) (text string , err error ) {
111
+ plaintext , err := DecryptByteChacha20poly1305WithNonceAppended (key , ciphertext )
112
+ if err != nil {
113
+ return
114
+ }
115
+
116
+ text = string (plaintext )
82
117
return
83
118
}
84
119
85
- // EncryptXChacha20poly1305 encrypts and authenticates the given message with
120
+ // EncryptByteXChacha20poly1305 encrypts and authenticates the given message (bytes) with
86
121
// XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
87
- func EncryptXChacha20poly1305 (key []byte , text string ) (ciphertext []byte , nonce []byte , err error ) {
122
+ func EncryptByteXChacha20poly1305 (key []byte , input [] byte ) (ciphertext []byte , nonce []byte , err error ) {
88
123
// create a new XChaCha20-Poly1305 AEAD using the given 256-bit key
89
124
aead , err := chacha20poly1305 .NewX (key )
90
125
if err != nil {
@@ -100,18 +135,20 @@ func EncryptXChacha20poly1305(key []byte, text string) (ciphertext []byte, nonce
100
135
return
101
136
}
102
137
103
- // data to be encrypted
104
- data := []byte (text )
105
-
106
138
// encrypt the data
107
- ciphertext = aead .Seal (nil , nonce , data , nil )
108
-
139
+ ciphertext = aead .Seal (nil , nonce , input , nil )
109
140
return
110
141
}
111
142
112
- // DecryptXChacha20poly1305 decrypts and authenticates the given message with
143
+ // EncryptXChacha20poly1305 encrypts and authenticates the given message (string) with
113
144
// XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
114
- func DecryptXChacha20poly1305 (key , nonce , ciphertext []byte ) (text string , err error ) {
145
+ func EncryptXChacha20poly1305 (key []byte , text string ) (ciphertext []byte , nonce []byte , err error ) {
146
+ return EncryptByteXChacha20poly1305 (key , []byte (text ))
147
+ }
148
+
149
+ // DecryptByteXChacha20poly1305 decrypts and authenticates the given ciphertext with
150
+ // XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
151
+ func DecryptByteXChacha20poly1305 (key , nonce , ciphertext []byte ) (plaintext []byte , err error ) {
115
152
// create a new XChaCha20-Poly1305 AEAD using the given 256-bit key
116
153
aead , err := chacha20poly1305 .NewX (key )
117
154
if err != nil {
@@ -120,38 +157,70 @@ func DecryptXChacha20poly1305(key, nonce, ciphertext []byte) (text string, err e
120
157
}
121
158
122
159
// decrypt the data
123
- plaintext , err : = aead .Open (nil , nonce , ciphertext , nil )
160
+ plaintext , err = aead .Open (nil , nonce , ciphertext , nil )
124
161
if err != nil {
125
162
err = fmt .Errorf ("error decrypting data: %v" , err )
126
163
return
127
164
}
128
- text = string (plaintext )
129
165
130
166
return
131
167
}
132
168
133
- // EncryptXChacha20poly1305WithNonceAppended encrypts and authenticates the given message with
169
+ // DecryptXChacha20poly1305 decrypts and authenticates the given ciphertext with
170
+ // XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
171
+ func DecryptXChacha20poly1305 (key , nonce , ciphertext []byte ) (text string , err error ) {
172
+ // decrypt the data
173
+ plaintext , err := DecryptByteXChacha20poly1305 (key , nonce , ciphertext )
174
+ if err != nil {
175
+ return
176
+ }
177
+
178
+ text = string (plaintext )
179
+ return
180
+ }
181
+
182
+ // EncryptByteXChacha20poly1305WithNonceAppended encrypts and authenticates the given message (bytes) with
134
183
// XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
135
184
// It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
136
- func EncryptXChacha20poly1305WithNonceAppended (key []byte , text string ) (ciphertext []byte , err error ) {
137
- ciphertext , nonce , err := EncryptXChacha20poly1305 (key , text )
185
+ func EncryptByteXChacha20poly1305WithNonceAppended (key []byte , input [] byte ) (ciphertext []byte , err error ) {
186
+ ciphertext , nonce , err := EncryptByteXChacha20poly1305 (key , input )
138
187
if err != nil {
139
188
return
140
189
}
141
190
ciphertext = append (nonce , ciphertext ... )
142
191
return
143
192
}
144
193
145
- // DecryptXChacha20poly1305WithNonceAppended decrypts and authenticates the given message with
194
+ // EncryptXChacha20poly1305WithNonceAppended encrypts and authenticates the given message (string) with
195
+ // XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
196
+ // It appends the ciphertext to the nonce [ciphertext = nonce + ciphertext].
197
+ func EncryptXChacha20poly1305WithNonceAppended (key []byte , text string ) (ciphertext []byte , err error ) {
198
+ return EncryptByteXChacha20poly1305WithNonceAppended (key , []byte (text ))
199
+ }
200
+
201
+ // DecryptByteXChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with
146
202
// XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
147
203
// It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
148
- func DecryptXChacha20poly1305WithNonceAppended (key , ciphertext []byte ) (text string , err error ) {
204
+ func DecryptByteXChacha20poly1305WithNonceAppended (key , ciphertext []byte ) (plaintext [] byte , err error ) {
149
205
nonceSize := chacha20poly1305 .NonceSizeX
150
206
if len (ciphertext ) < nonceSize {
151
207
err = errors .New ("ciphertext is too short" )
152
208
return
153
209
}
210
+
154
211
nonce , ciphertext := ciphertext [:nonceSize ], ciphertext [nonceSize :]
155
- text , err = DecryptXChacha20poly1305 (key , nonce , ciphertext )
212
+ return DecryptByteXChacha20poly1305 (key , nonce , ciphertext )
213
+ }
214
+
215
+ // DecryptXChacha20poly1305WithNonceAppended decrypts and authenticates the given ciphertext with
216
+ // XChaCha20-Poly1305 AEAD using the given 256-bit key and 192-bit nonce.
217
+ // It expects the ciphertext along with the nonce [ciphertext = nonce + ciphertext].
218
+ func DecryptXChacha20poly1305WithNonceAppended (key , ciphertext []byte ) (text string , err error ) {
219
+ plaintext , err := DecryptByteXChacha20poly1305WithNonceAppended (key , ciphertext )
220
+ if err != nil {
221
+ return
222
+ }
223
+
224
+ text = string (plaintext )
156
225
return
157
226
}
0 commit comments