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.

63 lines
1.5 KiB

  1. package crypto
  2. import "testing"
  3. import "encoding/hex"
  4. // convert a hex string to a key
  5. func HexToKey(h string) (result Key) {
  6. byteSlice, _ := hex.DecodeString(h)
  7. if len(byteSlice) != 32 {
  8. panic("Incorrect key size")
  9. }
  10. copy(result[:], byteSlice)
  11. return
  12. }
  13. func HexToHash(h string) (result Hash) {
  14. byteSlice, _ := hex.DecodeString(h)
  15. if len(byteSlice) != 32 {
  16. panic("Incorrect key size")
  17. }
  18. copy(result[:], byteSlice)
  19. return
  20. }
  21. func TestKeccak256(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. messageHex string
  25. wantHex string
  26. }{
  27. {
  28. name: "from monero 1",
  29. messageHex: "c8fedd380dbae40ffb52",
  30. wantHex: "8e41962058b7422e7404253121489a3e63d186ed115086919a75105661483ba9",
  31. },
  32. {
  33. name: "from monero 2",
  34. messageHex: "5020c4d530b6ec6cb4d9",
  35. wantHex: "8a597f11961935e32e0adeab2ce48b3df2d907c9b26619dad22f42ff65ab7593",
  36. },
  37. {
  38. name: "hello",
  39. messageHex: "68656c6c6f",
  40. wantHex: "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8",
  41. },
  42. {
  43. name: "from monero cryptotest.pl",
  44. messageHex: "0f3fe9c20b24a11bf4d6d1acd335c6a80543f1f0380590d7323caf1390c78e88",
  45. wantHex: "73b7a236f2a97c4e1805f7a319f1283e3276598567757186c526caf9a49e0a92",
  46. },
  47. }
  48. for _, test := range tests {
  49. message, _ := hex.DecodeString(test.messageHex)
  50. got := Keccak256(message)
  51. want := HexToHash(test.wantHex)
  52. if want != got {
  53. t.Errorf("want %x, got %x", want, got)
  54. }
  55. }
  56. }