-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsponge.go
69 lines (59 loc) · 1.3 KB
/
sponge.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
package sponge
import (
"crypto/sha256"
"encoding/hex"
"hash"
)
// Sponge instance
type Sponge struct {
state []byte
hash hash.Hash
}
// New creates a new sponge instance. You can specify any hash function which
// implements the hash.Hash interface. Default hash is SHA256.
func New(h hash.Hash) *Sponge {
if h == nil {
h = sha256.New()
}
s := &Sponge{hash: h}
s.transform()
return s
}
// transform sponge internal state
func (s *Sponge) transform() {
s.hash.Write(s.state)
s.state = s.hash.Sum(nil)
}
// absorb bytes
func (s *Sponge) absorb(buffer []byte) {
for _, b := range buffer {
s.state[0] = b ^ s.state[0]
s.transform()
}
}
// AbsorbByte absorbs a single byte into the sponge
func (s *Sponge) AbsorbByte(b byte) {
s.absorb([]byte{b})
}
// AbsorbBytes absorbs multiple bytes into the sponge
func (s *Sponge) AbsorbBytes(b []byte) {
s.absorb(b)
}
// Squeeze N bytes out of the sponge
func (s *Sponge) Squeeze(n int) []byte {
output := []byte{}
for i := 0; i < n; i++ {
b := s.state[0]
s.transform()
output = append(output, b)
}
return output
}
// String returns the sponge state hex encoded
func (s Sponge) String() string {
return hex.EncodeToString(s.state)
}
// Hex returns the sponge state hex encoded
func (s *Sponge) Hex() string {
return hex.EncodeToString(s.state)
}