From 64490897f3d7689fcd49dd3ae94b2b307f59b6cb Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 16 Mar 2015 17:27:24 +0100 Subject: crypto: add Sha3Hash --- crypto/crypto.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'crypto') diff --git a/crypto/crypto.go b/crypto/crypto.go index bc72928ac..1f8dfcf32 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -16,10 +16,10 @@ import ( "errors" "code.google.com/p/go-uuid/uuid" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/crypto/sha3" - "github.com/ethereum/go-ethereum/common" "golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/ripemd160" ) @@ -37,6 +37,15 @@ func Sha3(data ...[]byte) []byte { return d.Sum(nil) } +func Sha3Hash(data ...[]byte) (h common.Hash) { + d := sha3.NewKeccak256() + for _, b := range data { + d.Write(b) + } + d.Sum(h[:]) + return h +} + // Creates an ethereum address given the bytes and the nonce func CreateAddress(b []byte, nonce uint64) []byte { return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:] -- cgit v1.2.3 From ad78db4d62d392e9462ac4c1f8ac3d9718fdd0fc Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 17 Mar 2015 01:32:35 +0100 Subject: crypto: fix Sha3Hash and add a test for it --- crypto/crypto.go | 2 +- crypto/crypto_test.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'crypto') diff --git a/crypto/crypto.go b/crypto/crypto.go index 1f8dfcf32..ec2b41042 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -42,7 +42,7 @@ func Sha3Hash(data ...[]byte) (h common.Hash) { for _, b := range data { d.Write(b) } - d.Sum(h[:]) + d.Sum(h[:0]) return h } diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index 754287641..63a9c3f5e 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto/secp256k1" ) // These tests are sanity checks. @@ -21,6 +21,12 @@ func TestSha3(t *testing.T) { checkhash(t, "Sha3-256", func(in []byte) []byte { return Sha3(in) }, msg, exp) } +func TestSha3Hash(t *testing.T) { + msg := []byte("abc") + exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") + checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Sha3Hash(in); return h[:] }, msg, exp) +} + func TestSha256(t *testing.T) { msg := []byte("abc") exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") -- cgit v1.2.3 From 515d9432fcef8c574627049d437d6898b56c2829 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 17 Mar 2015 11:19:23 +0100 Subject: converted vm --- crypto/crypto.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'crypto') diff --git a/crypto/crypto.go b/crypto/crypto.go index 1f8dfcf32..e9582ac77 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -20,6 +20,7 @@ import ( "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/crypto/sha3" + "github.com/ethereum/go-ethereum/rlp" "golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/ripemd160" ) @@ -47,8 +48,10 @@ func Sha3Hash(data ...[]byte) (h common.Hash) { } // Creates an ethereum address given the bytes and the nonce -func CreateAddress(b []byte, nonce uint64) []byte { - return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:] +func CreateAddress(b common.Address, nonce uint64) common.Address { + data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}) + return common.BytesToAddress(Sha3(data)[12:]) + //return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:] } func Sha256(data []byte) []byte { -- cgit v1.2.3 From c388e7eac0d26f4d299523fd14ec350655f66879 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 20 Mar 2015 13:36:52 +0100 Subject: crypto: remove use of common.Value.Encode This seems to be the last remaining use of it. --- crypto/keypair.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'crypto') diff --git a/crypto/keypair.go b/crypto/keypair.go index 6702e6595..cc17328cb 100644 --- a/crypto/keypair.go +++ b/crypto/keypair.go @@ -3,8 +3,8 @@ package crypto import ( "strings" - "github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto/secp256k1" ) type KeyPair struct { @@ -48,11 +48,3 @@ func (k *KeyPair) Mnemonic() string { func (k *KeyPair) AsStrings() (string, string, string, string) { return k.Mnemonic(), common.Bytes2Hex(k.Address()), common.Bytes2Hex(k.PrivateKey), common.Bytes2Hex(k.PublicKey) } - -func (k *KeyPair) RlpEncode() []byte { - return k.RlpValue().Encode() -} - -func (k *KeyPair) RlpValue() *common.Value { - return common.NewValue(k.PrivateKey) -} -- cgit v1.2.3