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.

59 lines
1.3 KiB

  1. package cryptostore_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/go-keys/cryptostore"
  7. )
  8. func TestNoopEncoder(t *testing.T) {
  9. assert, require := assert.New(t), require.New(t)
  10. noop := cryptostore.Noop
  11. key := cryptostore.GenEd25519.Generate()
  12. key2 := cryptostore.GenSecp256k1.Generate()
  13. b, err := noop.Encrypt(key, "encode")
  14. require.Nil(err)
  15. assert.NotEmpty(b)
  16. b2, err := noop.Encrypt(key2, "encode")
  17. require.Nil(err)
  18. assert.NotEmpty(b2)
  19. assert.NotEqual(b, b2)
  20. // note the decode with a different password works - not secure!
  21. pk, err := noop.Decrypt(b, "decode")
  22. require.Nil(err)
  23. require.NotNil(pk)
  24. assert.Equal(key, pk)
  25. pk2, err := noop.Decrypt(b2, "kggugougp")
  26. require.Nil(err)
  27. require.NotNil(pk2)
  28. assert.Equal(key2, pk2)
  29. }
  30. func TestSecretBox(t *testing.T) {
  31. assert, require := assert.New(t), require.New(t)
  32. enc := cryptostore.SecretBox
  33. key := cryptostore.GenEd25519.Generate()
  34. pass := "some-special-secret"
  35. b, err := enc.Encrypt(key, pass)
  36. require.Nil(err)
  37. assert.NotEmpty(b)
  38. // decoding with a different pass is an error
  39. pk, err := enc.Decrypt(b, "decode")
  40. require.NotNil(err)
  41. require.Nil(pk)
  42. // but decoding with the same passphrase gets us our key
  43. pk, err = enc.Decrypt(b, pass)
  44. require.Nil(err)
  45. assert.Equal(key, pk)
  46. }