From bae27f6c6b85c3220d4dbd35c506d45917fdc169 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 7 Mar 2023 17:47:43 +0900 Subject: [PATCH 1/5] feat: sha1.Sum() --- stdlibs/crypto/sha1/sha1.gno | 19 +++++++++++++++++++ stdlibs/internal/crypto/sha1/sha1.gno | 3 +++ stdlibs/stdlibs.go | 17 ++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 stdlibs/crypto/sha1/sha1.gno create mode 100644 stdlibs/internal/crypto/sha1/sha1.gno diff --git a/stdlibs/crypto/sha1/sha1.gno b/stdlibs/crypto/sha1/sha1.gno new file mode 100644 index 00000000000..d26eb6b5313 --- /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 string) []byte { + return isha1.Sum(data) +} \ 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..e8c54ed357f 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -1,6 +1,7 @@ package stdlibs import ( + "crypto/sha1" "math" "reflect" "strconv" @@ -20,6 +21,20 @@ 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", "string", + ), + gno.Flds( // results + "bz", "[]byte", + ), + func(m *gno.Machine) { + arg0 := m.LastBlock().GetParams1().TV + hash := sha1.Sum([]byte(arg0.GetString())) + m.PushValue(typedByteArray(20, m.Alloc.NewArrayFromData(hash[:]))) + }, + ) case "internal/math": pn.DefineNative("Float32bits", gno.Flds( // params @@ -101,7 +116,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { } }, ) - // case "internal/os_test": + // case "internal/os_test": // XXX defined in tests/imports.go case "strconv": pn.DefineGoNativeValue("Itoa", strconv.Itoa) From b63fcc3ad6d245e122ed88c315420aa273b0ed4a Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 7 Mar 2023 18:24:55 +0900 Subject: [PATCH 2/5] feat: add tests --- examples/gno.land/r/demo/msha1/msha1.gno | 1 + examples/gno.land/r/demo/msha1/msha1_test.gno | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 examples/gno.land/r/demo/msha1/msha1.gno create mode 100644 examples/gno.land/r/demo/msha1/msha1_test.gno diff --git a/examples/gno.land/r/demo/msha1/msha1.gno b/examples/gno.land/r/demo/msha1/msha1.gno new file mode 100644 index 00000000000..1a12fe88531 --- /dev/null +++ b/examples/gno.land/r/demo/msha1/msha1.gno @@ -0,0 +1 @@ +package msha1 \ No newline at end of file diff --git a/examples/gno.land/r/demo/msha1/msha1_test.gno b/examples/gno.land/r/demo/msha1/msha1_test.gno new file mode 100644 index 00000000000..eeab6bd4a68 --- /dev/null +++ b/examples/gno.land/r/demo/msha1/msha1_test.gno @@ -0,0 +1,18 @@ +package msha1 + +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 From 57417357dc41c5c44de40f5fe1d268c0ba5496fc Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 7 Mar 2023 18:27:00 +0900 Subject: [PATCH 3/5] fix: indent typo --- stdlibs/stdlibs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlibs/stdlibs.go b/stdlibs/stdlibs.go index e8c54ed357f..ef24c4cb862 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -116,7 +116,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { } }, ) - // case "internal/os_test": + // case "internal/os_test": // XXX defined in tests/imports.go case "strconv": pn.DefineGoNativeValue("Itoa", strconv.Itoa) From 395bfdf3fd310bb1f71ea6f24ae3cf2e612acb69 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 7 Mar 2023 18:52:01 +0900 Subject: [PATCH 4/5] feat: change Sum() to take []byte instead of string --- examples/gno.land/r/demo/msha1/msha1_test.gno | 2 +- stdlibs/crypto/sha1/sha1.gno | 2 +- stdlibs/stdlibs.go | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/gno.land/r/demo/msha1/msha1_test.gno b/examples/gno.land/r/demo/msha1/msha1_test.gno index eeab6bd4a68..6fb8ac02fc8 100644 --- a/examples/gno.land/r/demo/msha1/msha1_test.gno +++ b/examples/gno.land/r/demo/msha1/msha1_test.gno @@ -12,7 +12,7 @@ func TestFunc(t *testing.T) { got := sha1.Sum([]byte("sha1 this string")) expected := "cf23df2207d99a74fbe169e3eba035e633b65d94" - if hex.EncodeToString(got) != expected { + 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/crypto/sha1/sha1.gno b/stdlibs/crypto/sha1/sha1.gno index d26eb6b5313..80154703b9e 100644 --- a/stdlibs/crypto/sha1/sha1.gno +++ b/stdlibs/crypto/sha1/sha1.gno @@ -14,6 +14,6 @@ import ( ) // Sum returns the SHA-1 checksum of the data. -func Sum(data string) []byte { +func Sum(data []byte) []byte { return isha1.Sum(data) } \ No newline at end of file diff --git a/stdlibs/stdlibs.go b/stdlibs/stdlibs.go index ef24c4cb862..187f7372311 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -24,15 +24,26 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { case "internal/crypto/sha1": pn.DefineNative("Sum", gno.Flds( // params - "data", "string", + "data", "[]byte", ), gno.Flds( // results "bz", "[]byte", ), func(m *gno.Machine) { arg0 := m.LastBlock().GetParams1().TV - hash := sha1.Sum([]byte(arg0.GetString())) - m.PushValue(typedByteArray(20, m.Alloc.NewArrayFromData(hash[:]))) + 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": From 4c42eec5aa7837b58550728c5727b98c7a208fc3 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 9 Mar 2023 14:16:34 +0900 Subject: [PATCH 5/5] chore: move test case to stdlibs --- examples/gno.land/r/demo/msha1/msha1.gno | 1 - .../msha1/msha1_test.gno => stdlibs/crypto/sha1/sha1_test.gno | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 examples/gno.land/r/demo/msha1/msha1.gno rename examples/gno.land/r/demo/msha1/msha1_test.gno => stdlibs/crypto/sha1/sha1_test.gno (95%) diff --git a/examples/gno.land/r/demo/msha1/msha1.gno b/examples/gno.land/r/demo/msha1/msha1.gno deleted file mode 100644 index 1a12fe88531..00000000000 --- a/examples/gno.land/r/demo/msha1/msha1.gno +++ /dev/null @@ -1 +0,0 @@ -package msha1 \ No newline at end of file diff --git a/examples/gno.land/r/demo/msha1/msha1_test.gno b/stdlibs/crypto/sha1/sha1_test.gno similarity index 95% rename from examples/gno.land/r/demo/msha1/msha1_test.gno rename to stdlibs/crypto/sha1/sha1_test.gno index 6fb8ac02fc8..1bfa4cdc042 100644 --- a/examples/gno.land/r/demo/msha1/msha1_test.gno +++ b/stdlibs/crypto/sha1/sha1_test.gno @@ -1,4 +1,4 @@ -package msha1 +package sha1 import ( "std"