-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstribog.go
79 lines (62 loc) · 1.15 KB
/
stribog.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
package stribog
import (
"sync"
bindings "github.com/dece2183/go-stribog/stribog_bindings"
)
const (
BlockSize = 64
)
type Stribog struct {
mux sync.RWMutex
data []byte
size int
}
func (s *Stribog) BlockSize() int {
return BlockSize
}
func (s *Stribog) Size() int {
return s.size
}
func (s *Stribog) Reset() {
s.mux.Lock()
s.data = s.data[:0]
s.mux.Unlock()
}
func (s *Stribog) Write(p []byte) (n int, err error) {
s.mux.Lock()
s.data = append(s.data, p...)
s.mux.Unlock()
return len(p), nil
}
func (s *Stribog) Sum(sum []byte) []byte {
var out []byte
s.mux.RLock()
in := make([]byte, len(s.data))
copy(in, s.data)
s.mux.RUnlock()
if s.size == 256/8 {
out = bindings.Hash256(in)
} else {
out = bindings.Hash512(in)
}
return append(sum, out...)
}
func (s *Stribog) CheckSum(p []byte) []byte {
if s.size == 256/8 {
return bindings.Hash256(p)
} else {
return bindings.Hash512(p)
}
}
func New256() *Stribog {
return &Stribog{size: 256 / 8}
}
func New512() *Stribog {
return &Stribog{size: 512 / 8}
}
func Sum256(p []byte) []byte {
return bindings.Hash256(p)
}
func Sum512(p []byte) []byte {
return bindings.Hash512(p)
}