2018-09-24 15:13:43 +02:00
|
|
|
package crypt
|
|
|
|
|
2019-04-27 20:15:38 +02:00
|
|
|
import (
|
|
|
|
"testing"
|
2018-09-24 15:13:43 +02:00
|
|
|
|
2019-04-27 20:15:38 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-11-18 16:50:43 +01:00
|
|
|
func BenchmarkEncrypt(b *testing.B) {
|
|
|
|
bob, _, _ := New([]byte("password"), nil)
|
2018-09-24 15:13:43 +02:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-11-18 16:50:43 +01:00
|
|
|
Encrypt([]byte("hello, world"), bob)
|
2019-04-27 20:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 15:13:43 +02:00
|
|
|
|
2019-11-18 16:50:43 +01:00
|
|
|
func BenchmarkDecrypt(b *testing.B) {
|
|
|
|
key, _, _ := New([]byte("password"), nil)
|
|
|
|
msg := []byte("hello, world")
|
|
|
|
enc, _ := Encrypt(msg, key)
|
|
|
|
b.ResetTimer()
|
2019-04-27 20:15:38 +02:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-11-18 16:50:43 +01:00
|
|
|
Decrypt(enc, key)
|
2018-09-24 15:13:43 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 20:15:38 +02:00
|
|
|
|
|
|
|
func TestEncryption(t *testing.T) {
|
2019-11-18 16:50:43 +01:00
|
|
|
key, salt, err := New([]byte("password"), nil)
|
2019-04-27 20:15:38 +02:00
|
|
|
assert.Nil(t, err)
|
2019-11-18 16:50:43 +01:00
|
|
|
msg := []byte("hello, world")
|
|
|
|
enc, err := Encrypt(msg, key)
|
2019-04-27 20:15:38 +02:00
|
|
|
assert.Nil(t, err)
|
2019-11-18 16:50:43 +01:00
|
|
|
dec, err := Decrypt(enc, key)
|
2019-04-28 02:03:05 +02:00
|
|
|
assert.Nil(t, err)
|
2019-11-18 16:50:43 +01:00
|
|
|
assert.Equal(t, msg, dec)
|
2019-04-30 00:33:15 +02:00
|
|
|
|
2019-11-18 16:50:43 +01:00
|
|
|
// check reusing the salt
|
|
|
|
key2, _, err := New([]byte("password"), salt)
|
|
|
|
dec, err = Decrypt(enc, key2)
|
2019-04-30 00:33:15 +02:00
|
|
|
assert.Nil(t, err)
|
2019-11-18 16:50:43 +01:00
|
|
|
assert.Equal(t, msg, dec)
|
2019-04-30 00:33:15 +02:00
|
|
|
|
2019-11-18 16:50:43 +01:00
|
|
|
// check reusing the salt
|
|
|
|
key2, _, err = New([]byte("wrong password"), salt)
|
|
|
|
dec, err = Decrypt(enc, key2)
|
2019-04-30 00:33:15 +02:00
|
|
|
assert.NotNil(t, err)
|
2019-11-18 16:50:43 +01:00
|
|
|
assert.NotEqual(t, msg, dec)
|
2019-09-07 18:41:24 +02:00
|
|
|
}
|