forked from spacemonkeygo/openssl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash.go
102 lines (87 loc) · 1.92 KB
/
hash.go
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
98
99
100
101
102
package openssl
// #include "shim.h"
import "C"
import (
"fmt"
"io"
"runtime"
"unsafe"
)
type Hash interface {
io.Writer
Reset() error
Sum() ([]byte, error)
Size() int
BlockSize() int
}
type internalHash struct {
ctx *C.EVP_MD_CTX
engine *Engine
digest *Digest
name string
size int
blockSize int
}
func newInternalHash(engine *Engine, nid NID, size int, blockSize int) (*internalHash, error) {
name, err := Nid2ShortName(nid)
if err != nil {
return nil, err
}
digest, err := GetDigestByName(name)
if err != nil {
return nil, err
}
ctx := C.X_EVP_MD_CTX_new()
if ctx == nil {
return nil, fmt.Errorf("openssl: %s: unable to allocate ctx", name)
}
result := &internalHash{
ctx: ctx,
engine: engine,
digest: digest,
name: name,
size: size,
blockSize: blockSize,
}
runtime.SetFinalizer(result, func(h *internalHash) { h.Close() })
if err := result.Reset(); err != nil {
return nil, err
}
return result, nil
}
func (h *internalHash) Close() {
if h.ctx != nil {
C.X_EVP_MD_CTX_free(h.ctx)
h.ctx = nil
}
}
func (h *internalHash) Reset() error {
if 1 != C.X_EVP_DigestInit_ex(h.ctx, h.digest.ptr, engineRef(h.engine)) {
return fmt.Errorf("openssl: %s: cannot init digest ctx", h.name)
}
return nil
}
func (h *internalHash) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if 1 != C.X_EVP_DigestUpdate(h.ctx, unsafe.Pointer(&p[0]),
C.size_t(len(p))) {
return 0, fmt.Errorf("openssl: %s: cannot update digest", h.name)
}
return len(p), nil
}
func (h *internalHash) Sum() ([]byte, error) {
result := make([]byte, h.Size())
if 1 != C.X_EVP_DigestFinal_ex(h.ctx,
(*C.uchar)(unsafe.Pointer(&result[0])), nil) {
return result, fmt.Errorf("openssl: %s: cannot finalize ctx", h.name)
}
return result, h.Reset()
}
func (h *internalHash) Size() int {
return h.size
}
func (h *internalHash) BlockSize() int {
return h.blockSize
}