starcrypto/sm3/sm3.go

47 lines
658 B
Go
Raw Normal View History

2024-03-10 13:04:26 +08:00
package sm3
import (
"hash"
gmsm3 "github.com/emmansun/gmsm/sm3"
2024-03-10 13:04:26 +08:00
)
type SM3 struct {
h hash.Hash
2024-03-10 13:04:26 +08:00
}
func New() hash.Hash {
s := &SM3{}
s.Reset()
return s
2024-03-10 13:04:26 +08:00
}
func (sm3 *SM3) BlockSize() int { return gmsm3.BlockSize }
2024-03-10 13:04:26 +08:00
func (sm3 *SM3) Size() int { return gmsm3.Size }
2024-03-10 13:04:26 +08:00
func (sm3 *SM3) Reset() {
sm3.h = gmsm3.New()
2024-03-10 13:04:26 +08:00
}
func (sm3 *SM3) Write(p []byte) (int, error) {
if sm3.h == nil {
sm3.Reset()
}
return sm3.h.Write(p)
2024-03-10 13:04:26 +08:00
}
func (sm3 *SM3) Sum(in []byte) []byte {
if sm3.h == nil {
sm3.Reset()
2024-03-10 13:04:26 +08:00
}
return sm3.h.Sum(in)
2024-03-10 13:04:26 +08:00
}
func Sm3Sum(data []byte) []byte {
sum := gmsm3.Sum(data)
out := make([]byte, len(sum))
copy(out, sum[:])
return out
2024-03-10 13:04:26 +08:00
}