diff --git a/multihash/multihash.go b/multihash/multihash.go index c459ce4..1811d3c 100644 --- a/multihash/multihash.go +++ b/multihash/multihash.go @@ -6,6 +6,7 @@ import ( "fmt" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "git.lumeweb.com/LumeWeb/libs5-go/types" + "git.lumeweb.com/LumeWeb/libs5-go/utils" "github.com/multiformats/go-multibase" "unicode/utf8" ) @@ -67,6 +68,7 @@ func (m *Multihash) Equals(other *Multihash) bool { } func (m *Multihash) HashCode() MultihashCode { + return utils.HashCode(m.FullBytes[:4]) return int(m.FullBytes[0]) + int(m.FullBytes[1])<<8 + int(m.FullBytes[2])<<16 + diff --git a/nodeid/nodeid.go b/nodeid/nodeid.go index 3f01db6..9c9030b 100644 --- a/nodeid/nodeid.go +++ b/nodeid/nodeid.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" + "git.lumeweb.com/LumeWeb/libs5-go/utils" "github.com/multiformats/go-multibase" ) @@ -40,13 +41,7 @@ func (nodeId *NodeId) Equals(other interface{}) bool { } func (nodeId *NodeId) HashCode() int { - if len(nodeId.Bytes) < 4 { - return 0 - } - return int(nodeId.Bytes[0]) + - int(nodeId.Bytes[1])<<8 + - int(nodeId.Bytes[2])<<16 + - int(nodeId.Bytes[3])<<24 + return utils.HashCode(nodeId.Bytes[:4]) } func (nodeId *NodeId) ToBase58() (string, error) { diff --git a/utils/bytes.go b/utils/bytes.go index 45c7817..b78e7b8 100644 --- a/utils/bytes.go +++ b/utils/bytes.go @@ -5,3 +5,14 @@ import "bytes" func ConcatBytes(slices ...[]byte) []byte { return bytes.Join(slices, nil) } + +func HashCode(bytes []byte) int { + if len(bytes) < 4 { + return 0 + } + + return int(bytes[0]) | + int(bytes[1])<<8 | + int(bytes[2])<<16 | + int(bytes[3])<<24 +}