storj/pkg/encryption/secretbox_test.go

41 lines
835 B
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-04-11 14:41:50 +01:00
// See LICENSE for copying information.
package encryption
2018-04-11 14:41:50 +01:00
import (
"bytes"
"io/ioutil"
"testing"
"storj.io/storj/private/testrand"
2018-04-11 14:41:50 +01:00
)
func TestSecretbox(t *testing.T) {
key := testrand.Key()
firstNonce := testrand.Nonce()
encrypter, err := NewSecretboxEncrypter(&key, &firstNonce, 4*1024)
2018-04-11 14:41:50 +01:00
if err != nil {
t.Fatal(err)
}
data := testrand.BytesInt(encrypter.InBlockSize() * 10)
encrypted := TransformReader(ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
decrypter, err := NewSecretboxDecrypter(&key, &firstNonce, 4*1024)
2018-04-11 14:41:50 +01:00
if err != nil {
t.Fatal(err)
}
decrypted := TransformReader(encrypted, decrypter, 0)
2018-04-11 14:41:50 +01:00
data2, err := ioutil.ReadAll(decrypted)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, data2) {
t.Fatalf("encryption/decryption failed")
}
}