-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsm3.go
More file actions
97 lines (82 loc) · 1.69 KB
/
Copy pathsm3.go
File metadata and controls
97 lines (82 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright Zhigui.com. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package gm_ossl
// #include <openssl/evp.h>
import "C"
import (
"errors"
"hash"
"runtime"
"unsafe"
)
type SM3Hash struct{}
type sm3Hash struct {
ctx *C.EVP_MD_CTX
engine *Engine
}
func SM3(data []byte) []byte {
sh := new(SM3Hash)
hasher := sh.NewSm3()
if _, err := hasher.Write(data); err != nil {
return nil
}
return hasher.Sum(nil)
}
func (h *SM3Hash) NewSm3() hash.Hash {
sm3 := new(sm3Hash)
sm3.ctx = C.EVP_MD_CTX_new()
if sm3.ctx == nil {
panic("openssl: sm3: unable to allocate ctx")
}
runtime.SetFinalizer(sm3, func(hash *sm3Hash) { hash.Close() })
sm3.Reset()
return sm3
}
func (s *sm3Hash) Close() {
if s.ctx != nil {
C.EVP_MD_CTX_free(s.ctx)
s.ctx = nil
}
}
func (s *sm3Hash) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if 1 != C.EVP_DigestUpdate(s.ctx, unsafe.Pointer(&p[0]),
C.size_t(len(p))) {
return 0, errors.New("openssl: sm3: cannot update digest")
}
return len(p), nil
}
func (s *sm3Hash) Sum(in []byte) []byte {
if len(in) > 0 {
if 1 != C.EVP_DigestUpdate(s.ctx, unsafe.Pointer(&in[0]), C.size_t(len(in))) {
return nil
}
}
out := make([]byte, 32)
var outlen C.uint
if 1 != C.EVP_DigestFinal(s.ctx, (*C.uchar)(unsafe.Pointer(&out[0])), &outlen) {
return nil
}
return out[:outlen]
}
func (s *sm3Hash) Reset() {
C.EVP_DigestInit_ex(s.ctx, C.EVP_sm3(), engineRef(s.engine))
}
func (s *sm3Hash) Size() int {
md := C.EVP_MD_CTX_md(s.ctx)
if md == nil {
return 0
}
return int(C.EVP_MD_size(md))
}
func (s *sm3Hash) BlockSize() int {
md := C.EVP_MD_CTX_md(s.ctx)
if md == nil {
return 0
}
return int(C.EVP_MD_block_size(md))
}