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.

103 lines
2.5 KiB

  1. package nano
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestParseDigest(t *testing.T) {
  9. assert, require := assert.New(t), require.New(t)
  10. cases := []struct {
  11. output string
  12. key string
  13. sig string
  14. valid bool
  15. }{
  16. {
  17. output: "80028E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C9300CAFE00787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
  18. key: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93",
  19. sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
  20. valid: true,
  21. },
  22. {
  23. output: "800235467890876543525437890796574535467890",
  24. key: "",
  25. sig: "",
  26. valid: false,
  27. },
  28. }
  29. for i, tc := range cases {
  30. msg, err := hex.DecodeString(tc.output)
  31. require.Nil(err, "%d: %+v", i, err)
  32. lKey, lSig, err := parseDigest(msg)
  33. if !tc.valid {
  34. assert.NotNil(err, "%d", i)
  35. } else if assert.Nil(err, "%d: %+v", i, err) {
  36. key, err := hex.DecodeString(tc.key)
  37. require.Nil(err, "%d: %+v", i, err)
  38. sig, err := hex.DecodeString(tc.sig)
  39. require.Nil(err, "%d: %+v", i, err)
  40. assert.Equal(key, lKey, "%d", i)
  41. assert.Equal(sig, lSig, "%d", i)
  42. }
  43. }
  44. }
  45. type cryptoCase struct {
  46. msg string
  47. key string
  48. sig string
  49. valid bool
  50. }
  51. func toBytes(c cryptoCase) (msg, key, sig []byte, err error) {
  52. msg, err = hex.DecodeString(c.msg)
  53. if err != nil {
  54. return
  55. }
  56. key, err = hex.DecodeString(c.key)
  57. if err != nil {
  58. return
  59. }
  60. sig, err = hex.DecodeString(c.sig)
  61. return
  62. }
  63. func TestCryptoConvert(t *testing.T) {
  64. assert, require := assert.New(t), require.New(t)
  65. cases := []cryptoCase{
  66. {
  67. msg: "F00D",
  68. key: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93",
  69. sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
  70. valid: true,
  71. },
  72. }
  73. for i, tc := range cases {
  74. msg, key, sig, err := toBytes(tc)
  75. require.Nil(err, "%d: %+v", i, err)
  76. pk, err := parseKey(key)
  77. require.Nil(err, "%d: %+v", i, err)
  78. psig, err := parseSig(sig)
  79. require.Nil(err, "%d: %+v", i, err)
  80. // it is not the signature of the message itself
  81. valid := pk.VerifyBytes(msg, psig)
  82. assert.NotEqual(tc.valid, valid, "%d", i)
  83. // but rather of the hash of the msg
  84. hmsg := hashMsg(msg)
  85. valid = pk.VerifyBytes(hmsg, psig)
  86. assert.Equal(tc.valid, valid, "%d", i)
  87. }
  88. }