Add support for NaCL SecretBox
This commit is contained in:
187
decrypt_test.go
Normal file
187
decrypt_test.go
Normal file
@@ -0,0 +1,187 @@
|
||||
package secret
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWithAES128GCM(t *testing.T) {
|
||||
key := []byte{
|
||||
0x37, 0xe4, 0x62, 0x59, 0xa6, 0xef, 0xe9, 0x96,
|
||||
0xb8, 0x5d, 0x2c, 0x35, 0xb5, 0x33, 0x3e, 0xff,
|
||||
}
|
||||
env := environment{"test": []byte{
|
||||
0x1c, 0x74, 0x37, 0xe9, 0x9e, 0x37, 0xb8, 0x9e,
|
||||
0xf0, 0x21, 0xc0, 0xec, 0xad, 0x9d, 0xdf, 0x67,
|
||||
0x75, 0xfd, 0x00, 0x48, 0x20, 0x46, 0x2e, 0x14,
|
||||
0xfb, 0x9d, 0x17, 0x9a, 0xe7, 0x9d, 0x6f, 0x39,
|
||||
0x19, 0xd5, 0x3f, 0x22, 0xa1, 0xac, 0xdb, 0xff,
|
||||
0x2c, 0x1f, 0x57, 0x0e, 0xbb, 0x5d, 0xff, 0x89,
|
||||
0x62, 0x55, 0x2b, 0x3b, 0x5e, 0xb5, 0x67, 0xb4,
|
||||
0x32, 0x92, 0x68, 0xcc, 0x55, 0x8e, 0xd3, 0xc7,
|
||||
0xce,
|
||||
}}
|
||||
|
||||
p, err := WithAESGCM(env, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := []byte("Gophers, gophers, gophers everywhere!")
|
||||
v, err := p.GetSecret("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
if !bytes.Equal(v, msg) {
|
||||
t.Errorf("expected:\n%s\n\ngot:\n%s", hex.Dump(msg), hex.Dump(v))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithAES256GCM(t *testing.T) {
|
||||
key := []byte{
|
||||
0x50, 0x0a, 0x33, 0xf2, 0xe5, 0x85, 0xc1, 0xd3,
|
||||
0x65, 0x6d, 0x32, 0xbe, 0xea, 0x7b, 0xce, 0x7e,
|
||||
0x12, 0xbf, 0x7a, 0x47, 0x4d, 0x21, 0x09, 0xa0,
|
||||
0x3e, 0x3f, 0x65, 0xc7, 0xae, 0x94, 0x6c, 0xe3,
|
||||
}
|
||||
env := environment{"test": []byte{
|
||||
0x62, 0x97, 0x6b, 0xc1, 0x78, 0xef, 0x41, 0xa0,
|
||||
0xd4, 0xdc, 0x05, 0x05, 0x66, 0xf6, 0x5f, 0x62,
|
||||
0xca, 0x91, 0xae, 0xd7, 0x7c, 0xff, 0xad, 0xc1,
|
||||
0xbf, 0xd5, 0x61, 0xe3, 0x09, 0x8d, 0x9c, 0xce,
|
||||
0xac, 0x88, 0x55, 0x8c, 0x02, 0xd4, 0x30, 0xc9,
|
||||
0x42, 0x38, 0xdf, 0xf9, 0x8a, 0x4c, 0x92, 0x34,
|
||||
0xc7, 0x82, 0x24, 0xb4, 0x9e, 0x9e, 0xdc, 0x10,
|
||||
0x96, 0x60, 0xa2, 0x92, 0x2a, 0x94, 0x9c, 0x3a,
|
||||
0xd8,
|
||||
}}
|
||||
|
||||
p, err := WithAESGCM(env, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := []byte("Gophers, gophers, gophers everywhere!")
|
||||
v, err := p.GetSecret("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
if !bytes.Equal(v, msg) {
|
||||
t.Errorf("expected:\n%s\n\ngot:\n%s", hex.Dump(msg), hex.Dump(v))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithChaCha20Poly1305(t *testing.T) {
|
||||
key := []byte{
|
||||
0x01, 0x94, 0x99, 0xd6, 0x89, 0x82, 0x7f, 0xa1,
|
||||
0x27, 0x82, 0x36, 0x11, 0x78, 0x22, 0xe5, 0x16,
|
||||
0x21, 0xe2, 0xc6, 0x29, 0x0c, 0x14, 0x6c, 0xb8,
|
||||
0x12, 0x46, 0x2f, 0xae, 0x9e, 0xa7, 0x17, 0x09,
|
||||
}
|
||||
|
||||
env := environment{"test": []byte{
|
||||
0x76, 0x7c, 0x7d, 0x5d, 0x36, 0xd1, 0x27, 0xca,
|
||||
0x0b, 0x64, 0x28, 0x82, 0xc3, 0x3a, 0xd3, 0x24,
|
||||
0x30, 0x77, 0x8f, 0xef, 0xf1, 0x3a, 0x9f, 0xa7,
|
||||
0x41, 0x49, 0x62, 0x79, 0x9f, 0x65, 0x12, 0x4f,
|
||||
0x29, 0xe6, 0x04, 0x83, 0xab, 0x5c, 0xbc, 0x0b,
|
||||
0xe2, 0x82, 0xcc, 0x3c, 0x47, 0x7d, 0xaa, 0x6d,
|
||||
0x4c, 0x71, 0x6b, 0x24, 0x01, 0xd0, 0xf9, 0x88,
|
||||
0xcf, 0x88, 0xbe, 0xee, 0xe2, 0x77, 0x07, 0x18,
|
||||
0xf1,
|
||||
}}
|
||||
|
||||
p, err := WithChaCha20Poly1305(env, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := []byte("Gophers, gophers, gophers everywhere!")
|
||||
v, err := p.GetSecret("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
if !bytes.Equal(v, msg) {
|
||||
t.Errorf("expected:\n%s\n\ngot:\n%s", hex.Dump(msg), hex.Dump(v))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithChaCha20Poly1305X(t *testing.T) {
|
||||
key := []byte{
|
||||
0x25, 0x20, 0xda, 0x7c, 0x97, 0x60, 0x20, 0xf5,
|
||||
0x09, 0xa7, 0x42, 0x31, 0x08, 0x50, 0x76, 0xf6,
|
||||
0x79, 0xc5, 0x38, 0x5b, 0xfb, 0xf4, 0x98, 0x56,
|
||||
0xa1, 0x92, 0xd1, 0xa0, 0x08, 0x3e, 0xf5, 0xfd,
|
||||
}
|
||||
|
||||
env := environment{"test": []byte{
|
||||
0x59, 0xaf, 0xc4, 0x65, 0x60, 0x3f, 0x02, 0xd3,
|
||||
0x87, 0xd3, 0x15, 0xac, 0xf3, 0x6b, 0x4a, 0x4f,
|
||||
0xe8, 0x40, 0xe5, 0x4d, 0x80, 0x30, 0x7b, 0x3d,
|
||||
0x8a, 0xf4, 0x18, 0x96, 0x89, 0x05, 0x4e, 0x31,
|
||||
0x44, 0x8c, 0xb2, 0x84, 0x9d, 0x25, 0xce, 0x3b,
|
||||
0xb4, 0x66, 0x36, 0x0f, 0xd2, 0xad, 0xb3, 0x78,
|
||||
0xf8, 0x02, 0x1e, 0x6c, 0xf9, 0x6c, 0x1f, 0x71,
|
||||
0x3c, 0x2b, 0x59, 0x6f, 0xc7, 0x92, 0x3b, 0x40,
|
||||
0x89, 0x13, 0x93, 0x6b, 0xa0, 0x35, 0x4e, 0x6f,
|
||||
0xd8, 0x31, 0x67, 0xee, 0xa2,
|
||||
}}
|
||||
|
||||
p, err := WithChaCha20Poly1305X(env, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := []byte("Gophers, gophers, gophers everywhere!")
|
||||
v, err := p.GetSecret("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
if !bytes.Equal(v, msg) {
|
||||
t.Errorf("expected:\n%s\n\ngot:\n%s", hex.Dump(msg), hex.Dump(v))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithSecretBox(t *testing.T) {
|
||||
var (
|
||||
key [32]byte
|
||||
nonce [24]byte
|
||||
box []byte
|
||||
)
|
||||
key = [32]uint8{
|
||||
0x90, 0x93, 0x6b, 0x91, 0x79, 0xac, 0xa2, 0x0d,
|
||||
0x7d, 0xf7, 0x8c, 0x29, 0x93, 0x5f, 0xd4, 0xf3,
|
||||
0x9e, 0x61, 0xe3, 0x6d, 0xb0, 0x30, 0x31, 0xac,
|
||||
0xa5, 0xd6, 0x7e, 0xb5, 0x04, 0xe8, 0x1b, 0x87,
|
||||
}
|
||||
nonce = [24]uint8{
|
||||
0xaa, 0xe6, 0x05, 0x6b, 0x18, 0xb9, 0x05, 0xc8,
|
||||
0x9e, 0x53, 0x25, 0x79, 0xbe, 0x79, 0x9d, 0x9b,
|
||||
0xbe, 0x1e, 0x34, 0x11, 0xbb, 0x21, 0x51, 0x3c,
|
||||
}
|
||||
box = append(nonce[:],
|
||||
0x8b, 0x0a, 0x66, 0x4f, 0xb3, 0x1f, 0x78, 0xfd,
|
||||
0xcb, 0x41, 0x9e, 0x4b, 0x1a, 0x9e, 0x34, 0x20,
|
||||
0xef, 0x2c, 0x64, 0xb3, 0x00, 0x6f, 0xf1, 0x9e,
|
||||
0xe1, 0xbd, 0xfe, 0xd3, 0x86, 0x12, 0x8d, 0x09,
|
||||
0x85, 0x34, 0xae, 0xa3, 0xfd)
|
||||
|
||||
p := WithSecretBox(environment{"test": box}, key)
|
||||
v, err := p.GetSecret("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
if string(v) != "Hello, boxed Gophers!" {
|
||||
t.Errorf(`expected "Hello, boxed Gophers!", got %q`, v)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user