Browse Source

crypto: make it easier to run Sha3 on multiple inputs

crypto.Sha3(append(foo, bar)) --> crypto.Sha3(foo, bar)
crypto.Sha3([]byte{}) --> crypto.Sha3()
Felix Lange 10 years ago
parent
commit
410b35e913
2 changed files with 5 additions and 4 deletions
  1. 4 3
      crypto/crypto.go
  2. 1 1
      crypto/crypto_test.go

+ 4 - 3
crypto/crypto.go

@@ -27,10 +27,11 @@ func init() {
 	ecies.AddParamsForCurve(S256(), ecies.ECIES_AES128_SHA256)
 }
 
-func Sha3(data []byte) []byte {
+func Sha3(data ...[]byte) []byte {
 	d := sha3.NewKeccak256()
-	d.Write(data)
-
+	for _, b := range data {
+		d.Write(b)
+	}
 	return d.Sum(nil)
 }
 

+ 1 - 1
crypto/crypto_test.go

@@ -18,7 +18,7 @@ import (
 func TestSha3(t *testing.T) {
 	msg := []byte("abc")
 	exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
-	checkhash(t, "Sha3-256", Sha3, msg, exp)
+	checkhash(t, "Sha3-256", func(in []byte) []byte { return Sha3(in) }, msg, exp)
 }
 
 func TestSha256(t *testing.T) {