crypto_test.go 1017 B

123456789101112131415161718192021222324252627282930313233343536
  1. package crypto
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. )
  7. // These tests are sanity checks.
  8. // They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
  9. // and that the sha3 library uses keccak-f permutation.
  10. func TestSha3(t *testing.T) {
  11. msg := []byte("abc")
  12. exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
  13. checkhash(t, "Sha3-256", Sha3, msg, exp)
  14. }
  15. func TestSha256(t *testing.T) {
  16. msg := []byte("abc")
  17. exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
  18. checkhash(t, "Sha256", Sha256, msg, exp)
  19. }
  20. func TestRipemd160(t *testing.T) {
  21. msg := []byte("abc")
  22. exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc")
  23. checkhash(t, "Ripemd160", Ripemd160, msg, exp)
  24. }
  25. func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) {
  26. sum := f(msg)
  27. if bytes.Compare(exp, sum) != 0 {
  28. t.Errorf("hash %s returned wrong result.\ngot: %x\nwant: %x", name, sum, exp)
  29. }
  30. }