Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed May 13, 2024
1 parent ca05e9a commit ce9bc02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions hash/sm3/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func (this *digest) MarshalBinary() ([]byte, error) {

func (this *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || (string(b[:len(magic)]) != magic) {
return errors.New("go-hahs/sm3: invalid hash state identifier")
return errors.New("go-cryptobin/sm3: invalid hash state identifier")
}

if len(b) != marshaledSize {
return errors.New("go-hahs/sm3: invalid hash state size")
return errors.New("go-cryptobin/sm3: invalid hash state size")
}

b = b[len(magic):]
Expand Down
26 changes: 19 additions & 7 deletions hash/sm3/binary_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package sm3

import (
"fmt"
"bytes"
"testing"
)

func Test_MarshalBinary(t *testing.T) {
msg := []byte("test-dd1111111dddddddatatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datatest-data")

h := new(digest)
h.Reset()
msg := []byte("abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd")
check := "90d52a2e85631a8d6035262626941fa11b85ce570cec1e3e991e2dd7ed258148"

h := newDigest()
h.Write(msg)
dst := h.Sum(nil)
if len(dst) == 0 {
Expand All @@ -19,18 +19,30 @@ func Test_MarshalBinary(t *testing.T) {

bs, _ := h.MarshalBinary()

h.Reset()
err := h.UnmarshalBinary(bs)
h2 := newDigest()
err := h2.UnmarshalBinary(bs)
if err != nil {
t.Fatal(err)
}

newdst := h.Sum(nil)
newdst := h2.Sum(nil)
if len(newdst) == 0 {
t.Error("newHash make error")
}

if !bytes.Equal(newdst, dst) {
t.Errorf("Hash MarshalBinary error, got %x, want %x", newdst, dst)
}

// ===========

sum1 := fmt.Sprintf("%x", dst)
if sum1 != check {
t.Errorf("Sum error, got %s, want %s", sum1, check)
}

sum2 := fmt.Sprintf("%x", newdst)
if sum2 != check {
t.Errorf("ReSum error, got %s, want %s", sum2, check)
}
}

0 comments on commit ce9bc02

Please sign in to comment.