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 939ec94
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
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
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
}
}

0 comments on commit 939ec94

Please sign in to comment.