diff --git a/stdlibs/crypto/sha1/sha1.gno b/stdlibs/crypto/sha1/sha1.gno new file mode 100644 index 00000000000..80154703b9e --- /dev/null +++ b/stdlibs/crypto/sha1/sha1.gno @@ -0,0 +1,19 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package sha1 implements the SHA-1 hash algorithm as defined in RFC 3174. +// +// SHA-1 is cryptographically broken and should not be used for secure +// applications. + +package sha1 + +import ( + isha1 "internal/crypto/sha1" // XXX +) + +// Sum returns the SHA-1 checksum of the data. +func Sum(data []byte) []byte { + return isha1.Sum(data) +} \ No newline at end of file diff --git a/stdlibs/crypto/sha1/sha1_test.gno b/stdlibs/crypto/sha1/sha1_test.gno new file mode 100644 index 00000000000..1bfa4cdc042 --- /dev/null +++ b/stdlibs/crypto/sha1/sha1_test.gno @@ -0,0 +1,18 @@ +package sha1 + +import ( + "std" + "testing" + + "crypto/sha1" + "encoding/hex" +) + +func TestFunc(t *testing.T) { + got := sha1.Sum([]byte("sha1 this string")) + expected := "cf23df2207d99a74fbe169e3eba035e633b65d94" + + if (hex.EncodeToString(got)) != expected { + t.Errorf("got %v(%T), expected %v(%T)", got, got, expected, expected) + } +} \ No newline at end of file diff --git a/stdlibs/internal/crypto/sha1/sha1.gno b/stdlibs/internal/crypto/sha1/sha1.gno new file mode 100644 index 00000000000..954d749bc69 --- /dev/null +++ b/stdlibs/internal/crypto/sha1/sha1.gno @@ -0,0 +1,3 @@ +package sha1 + +// XXX injected via stdlibs/stdlibs.go diff --git a/stdlibs/stdlibs.go b/stdlibs/stdlibs.go index b365e43a14e..187f7372311 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -1,6 +1,7 @@ package stdlibs import ( + "crypto/sha1" "math" "reflect" "strconv" @@ -20,6 +21,31 @@ func InjectNativeMappings(store gno.Store) { func InjectPackage(store gno.Store, pn *gno.PackageNode) { switch pn.PkgPath { + case "internal/crypto/sha1": + pn.DefineNative("Sum", + gno.Flds( // params + "data", "[]byte", + ), + gno.Flds( // results + "bz", "[]byte", + ), + func(m *gno.Machine) { + arg0 := m.LastBlock().GetParams1().TV + bz := []byte(nil) + if arg0.V != nil { + slice := arg0.V.(*gno.SliceValue) + array := slice.GetBase(m.Store) + bz = array.GetReadonlyBytes() + } + hash := sha1.Sum(bz) + res0 := gno.Go2GnoValue( + m.Alloc, + m.Store, + reflect.ValueOf([20]byte(hash)), + ) + m.PushValue(res0) + }, + ) case "internal/math": pn.DefineNative("Float32bits", gno.Flds( // params