Skip to content

Commit

Permalink
fix: Fix blobHash collision
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Sep 18, 2023
1 parent 9a8f776 commit dc09e01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vm/core/blob/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func mustGetBlobHash(fields dict.Dict) (hashing.HashValue, []kv.Key, [][]byte) {
for _, k := range sorted {
v := fields.Get(k)
values = append(values, v)
all = append(all, v)
all = append(all, []byte(k))
all = append(all, v)
}
return hashing.HashData(all...), sorted, values
}
Expand Down
26 changes: 26 additions & 0 deletions packages/vm/core/blob/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package blob

import (
"encoding/hex"
"testing"

"github.com/iotaledger/wasp/packages/kv/dict"

Check failure on line 7 in packages/vm/core/blob/internal_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed with -local github.com/iotaledger (goimports)
"github.com/stretchr/testify/require"
)

func TestMustGetBlobHash(t *testing.T) {
fields := dict.Dict{
"field1": []byte("value1"),
"field2": []byte("value2"),
}

h, keys, values := mustGetBlobHash(fields)
for i, k := range keys {
require.Equal(t, fields[k], values[i])
}

// blobHash = hash(fieldName1 || binaryChunk1 || fieldName2 || binaryChunk2 || ... || fieldNameN || binaryChunkN)
resHash, err := hex.DecodeString("d45cd5dee59f83b3334c1acce7a38f12280d264aada764ed0d65e8a88c4693b0")
require.NoError(t, err)
require.Equal(t, resHash, h.Bytes())
}

0 comments on commit dc09e01

Please sign in to comment.