You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

  1. package xsalsa20symmetric
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "golang.org/x/crypto/bcrypt" // forked to github.com/tendermint/crypto
  7. "github.com/tendermint/tendermint/crypto"
  8. )
  9. func TestSimple(t *testing.T) {
  10. crypto.MixEntropy([]byte("someentropy"))
  11. plaintext := []byte("sometext")
  12. secret := []byte("somesecretoflengththirtytwo===32")
  13. ciphertext := EncryptSymmetric(plaintext, secret)
  14. plaintext2, err := DecryptSymmetric(ciphertext, secret)
  15. require.Nil(t, err, "%+v", err)
  16. assert.Equal(t, plaintext, plaintext2)
  17. }
  18. func TestSimpleWithKDF(t *testing.T) {
  19. crypto.MixEntropy([]byte("someentropy"))
  20. plaintext := []byte("sometext")
  21. secretPass := []byte("somesecret")
  22. salt := []byte("somesaltsomesalt") // len 16
  23. // NOTE: we use a fork of x/crypto so we can inject our own randomness for salt
  24. secret, err := bcrypt.GenerateFromPassword(salt, secretPass, 12)
  25. if err != nil {
  26. t.Error(err)
  27. }
  28. secret = crypto.Sha256(secret)
  29. ciphertext := EncryptSymmetric(plaintext, secret)
  30. plaintext2, err := DecryptSymmetric(ciphertext, secret)
  31. require.Nil(t, err, "%+v", err)
  32. assert.Equal(t, plaintext, plaintext2)
  33. }