github.com/karalabe/go-ethereum@v0.8.5/crypto/crypto_test.go (about) 1 package crypto 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "fmt" 7 "testing" 8 "time" 9 10 "github.com/ethereum/go-ethereum/crypto/secp256k1" 11 "github.com/ethereum/go-ethereum/ethutil" 12 ) 13 14 // These tests are sanity checks. 15 // They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256 16 // and that the sha3 library uses keccak-f permutation. 17 18 func TestSha3(t *testing.T) { 19 msg := []byte("abc") 20 exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") 21 checkhash(t, "Sha3-256", func(in []byte) []byte { return Sha3(in) }, msg, exp) 22 } 23 24 func TestSha256(t *testing.T) { 25 msg := []byte("abc") 26 exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") 27 checkhash(t, "Sha256", Sha256, msg, exp) 28 } 29 30 func TestRipemd160(t *testing.T) { 31 msg := []byte("abc") 32 exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc") 33 checkhash(t, "Ripemd160", Ripemd160, msg, exp) 34 } 35 36 func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) { 37 sum := f(msg) 38 if bytes.Compare(exp, sum) != 0 { 39 t.Errorf("hash %s returned wrong result.\ngot: %x\nwant: %x", name, sum, exp) 40 } 41 } 42 43 func BenchmarkSha3(b *testing.B) { 44 a := []byte("hello world") 45 amount := 1000000 46 start := time.Now() 47 for i := 0; i < amount; i++ { 48 Sha3(a) 49 } 50 51 fmt.Println(amount, ":", time.Since(start)) 52 } 53 54 func Test0Key(t *testing.T) { 55 t.Skip() 56 key := ethutil.Hex2Bytes("1111111111111111111111111111111111111111111111111111111111111111") 57 58 p, err := secp256k1.GeneratePubKey(key) 59 addr := Sha3(p[1:])[12:] 60 fmt.Printf("%x\n", p) 61 fmt.Printf("%v %x\n", err, addr) 62 } 63 64 func TestInvalidSign(t *testing.T) { 65 _, err := Sign(make([]byte, 1), nil) 66 if err == nil { 67 t.Errorf("expected sign with hash 1 byte to error") 68 } 69 70 _, err = Sign(make([]byte, 33), nil) 71 if err == nil { 72 t.Errorf("expected sign with hash 33 byte to error") 73 } 74 }