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.

48 lines
986 B

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package crypto
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestSignAndValidateEd25519(t *testing.T) {
  8. privKey := GenPrivKeyEd25519()
  9. pubKey, err := privKey.PubKey()
  10. require.Nil(t, err)
  11. msg := CRandBytes(128)
  12. sig, err := privKey.Sign(msg)
  13. require.Nil(t, err)
  14. // Test the signature
  15. assert.True(t, pubKey.VerifyBytes(msg, sig))
  16. // Mutate the signature, just one bit.
  17. sigEd := sig.(SignatureEd25519)
  18. sigEd[7] ^= byte(0x01)
  19. sig = sigEd
  20. assert.False(t, pubKey.VerifyBytes(msg, sig))
  21. }
  22. func TestSignAndValidateSecp256k1(t *testing.T) {
  23. privKey := GenPrivKeySecp256k1()
  24. pubKey, err := privKey.PubKey()
  25. require.Nil(t, err)
  26. msg := CRandBytes(128)
  27. sig, err := privKey.Sign(msg)
  28. require.Nil(t, err)
  29. assert.True(t, pubKey.VerifyBytes(msg, sig))
  30. // Mutate the signature, just one bit.
  31. sigEd := sig.(SignatureSecp256k1)
  32. sigEd[3] ^= byte(0x01)
  33. sig = sigEd
  34. assert.False(t, pubKey.VerifyBytes(msg, sig))
  35. }