Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sha1.Sum() #570

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/msha1/msha1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package msha1
18 changes: 18 additions & 0 deletions examples/gno.land/r/demo/msha1/msha1_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package msha1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this test to the stdlibs folder, or you can keep it here and create an official example folder to test the stdlibs. If you choose to keep it here, I suggest moving it outside of the r/ folder. What about gno.land/p/demo/stdlibs_tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving test to stdlibs folder looks more straightforward to me. I'll move it


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)
}
}
19 changes: 19 additions & 0 deletions stdlibs/crypto/sha1/sha1.gno
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions stdlibs/internal/crypto/sha1/sha1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package sha1

// XXX injected via stdlibs/stdlibs.go
26 changes: 26 additions & 0 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stdlibs

import (
"crypto/sha1"
"math"
"reflect"
"strconv"
Expand All @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider using

bz = array.GetReadonlyBytes()[:slice.Length]

The function GetReadonlyBytes() could return leading 0 in the bytes array.

}
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
Expand Down