2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-05-15 16:11:03 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-18 12:10:29 +01:00
|
|
|
package encryption
|
2018-05-15 16:11:03 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2018-10-19 14:38:13 +01:00
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testrand"
|
2018-05-15 16:11:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAesGcm(t *testing.T) {
|
2019-06-26 11:38:51 +01:00
|
|
|
key := testrand.Key()
|
2018-09-26 14:32:23 +01:00
|
|
|
var firstNonce AESGCMNonce
|
2019-06-26 11:38:51 +01:00
|
|
|
testrand.Read(firstNonce[:])
|
|
|
|
|
2018-05-15 16:11:03 +01:00
|
|
|
encrypter, err := NewAESGCMEncrypter(&key, &firstNonce, 4*1024)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-26 11:38:51 +01:00
|
|
|
|
|
|
|
data := testrand.BytesInt(encrypter.InBlockSize() * 10)
|
|
|
|
encrypted := TransformReader(ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
|
2018-05-15 16:11:03 +01:00
|
|
|
decrypter, err := NewAESGCMDecrypter(&key, &firstNonce, 4*1024)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
decrypted := TransformReader(encrypted, decrypter, 0)
|
|
|
|
data2, err := ioutil.ReadAll(decrypted)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-26 11:38:51 +01:00
|
|
|
|
2018-05-15 16:11:03 +01:00
|
|
|
if !bytes.Equal(data, data2) {
|
|
|
|
t.Fatalf("encryption/decryption failed")
|
|
|
|
}
|
|
|
|
}
|