Skip to content

Commit

Permalink
Add murmur3 to hash module
Browse files Browse the repository at this point in the history
  • Loading branch information
aadi-stripe committed Sep 3, 2024
1 parent 985d3a7 commit 4368b14
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
6 changes: 6 additions & 0 deletions build/go_dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def go_dependencies():
sum = "h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=",
version = "v1.0.0",
)
go_repository(
name = "com_github_spaolacci_murmur3",
importpath = "github.com/spaolacci/murmur3",
sum = "h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=",
version = "v1.1.0",
)
go_repository(
name = "in_gopkg_check_v1",
importpath = "gopkg.in/check.v1",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/stripe/skycfg
go 1.16

require (
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.starlark.net v0.0.0-20201204201740-42d4f566359b
google.golang.org/protobuf v1.25.0
gopkg.in/yaml.v2 v2.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
go.starlark.net v0.0.0-20201204201740-42d4f566359b h1:yHUzJ1WfcdR1oOafytJ6K1/ntYwnEIXICNVzHb+FzbA=
go.starlark.net v0.0.0-20201204201740-42d4f566359b/go.mod h1:5YFcFnRptTN+41758c2bMPiqpGg4zBfYji1IQz8wNFk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
1 change: 1 addition & 0 deletions go/hashmodule/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
importpath = "github.com/stripe/skycfg/go/hashmodule",
visibility = ["//visibility:public"],
deps = [
"@com_github_spaolacci_murmur3//:go_default_library",
"@net_starlark_go//starlark",
"@net_starlark_go//starlarkstruct",
],
Expand Down
35 changes: 27 additions & 8 deletions go/hashmodule/hashmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,32 @@ import (
"fmt"
"hash"

"github.com/spaolacci/murmur3"

"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)

// NewModule returns a Starlark module of common hash functions.
//
// hash = module(
// md5,
// sha1,
// sha256,
// )
// hash = module(
//
// md5,
// sha1,
// sha256,
// murmur3,
//
// )
//
// See `docs/modules.asciidoc` for details on the API of each function.
func NewModule() *starlarkstruct.Module {
return &starlarkstruct.Module{
Name: "hash",
Members: starlark.StringDict{
"md5": starlark.NewBuiltin("hash.md5", fnHash(md5.New)),
"sha1": starlark.NewBuiltin("hash.sha1", fnHash(sha1.New)),
"sha256": starlark.NewBuiltin("hash.sha256", fnHash(sha256.New)),
"md5": starlark.NewBuiltin("hash.md5", fnHash(md5.New)),
"sha1": starlark.NewBuiltin("hash.sha1", fnHash(sha1.New)),
"sha256": starlark.NewBuiltin("hash.sha256", fnHash(sha256.New)),
"murmur3": starlark.NewBuiltin("hash.murmur3", fnHash64(murmur3.New64)),
},
}
}
Expand All @@ -60,3 +66,16 @@ func fnHash(hash func() hash.Hash) func(*starlark.Thread, *starlark.Builtin, sta
return starlark.String(fmt.Sprintf("%x", h.Sum(nil))), nil
}
}

func fnHash64(hash func() hash.Hash64) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
return func(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var s starlark.String
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &s); err != nil {
return nil, err
}

h := hash()
h.Write([]byte(string(s)))
return starlark.String(fmt.Sprintf("%x", h.Sum(nil))), nil
}
}
5 changes: 5 additions & 0 deletions go/hashmodule/hashmodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func TestHashes(t *testing.T) {
hashFunc: "sha256",
expOutput: "a9c78816353b119a0ba2a1281675b147fd47abee11a8d41d5abb739dce8273b7",
},
hashTestCase{
input: "test murmur3 string",
hashFunc: "murmur3",
expOutput: "91dd87efc613eb2c",
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit 4368b14

Please sign in to comment.