-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from smintz/hash
- Loading branch information
Showing
7 changed files
with
145 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# time | ||
|
||
hash defines hash primitives for starlark | ||
|
||
## Functions | ||
|
||
#### `md5(string) string` | ||
|
||
returns md5 hash of a string | ||
|
||
#### `sha1(string) string` | ||
|
||
returns sha1 hash of a string | ||
|
||
#### `sha256(string) string` | ||
|
||
returns sha256 hash of a string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/*Package hash defines hash primitives for starlark. | ||
outline: | ||
hash defines hash primitives for starlark. | ||
path: hash | ||
functions: | ||
md5(string) string | ||
returns an md5 hash for a string | ||
sha1(string) string | ||
returns an sha1 hash for a string | ||
sha256(string) string | ||
returns an sha256 hash for a string | ||
*/ | ||
package hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package hash | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"fmt" | ||
"hash" | ||
"sync" | ||
|
||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarkstruct" | ||
) | ||
|
||
// ModuleName defines the expected name for this Module when used | ||
// in starlark's load() function, eg: load('hash.star', 'hash') | ||
const ModuleName = "hash.star" | ||
|
||
var ( | ||
once sync.Once | ||
hashModule starlark.StringDict | ||
hashError error | ||
) | ||
|
||
// LoadModule loads the time module. | ||
// It is concurrency-safe and idempotent | ||
func LoadModule() (starlark.StringDict, error) { | ||
once.Do(func() { | ||
hashModule = starlark.StringDict{ | ||
"hash": &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)), | ||
}, | ||
}, | ||
} | ||
|
||
}) | ||
return hashModule, hashError | ||
|
||
} | ||
|
||
func fnHash(hash func() hash.Hash) 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package hash | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/qri-io/starlib/testdata" | ||
"go.starlark.net/resolve" | ||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarktest" | ||
) | ||
|
||
func TestFile(t *testing.T) { | ||
resolve.AllowFloat = true | ||
thread := &starlark.Thread{Load: testdata.NewLoader(LoadModule, ModuleName)} | ||
starlarktest.SetReporter(thread, t) | ||
|
||
// Execute test file | ||
_, err := starlark.ExecFile(thread, "testdata/test.star", nil, nil) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" | ||
Test data for hash module | ||
""" | ||
load('hash.star', 'hash') | ||
load('assert.star', 'assert') | ||
|
||
assert.eq(hash.md5("helloworld"), "fc5e038d38a57032085441e7fe7010b0") | ||
assert.eq(hash.sha1("helloworld"), "6adfb183a4a2c94a2f92dab5ade762a47889a5a1") | ||
assert.eq(hash.sha256("helloworld"), "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters