/
hash.go
74 lines (55 loc) · 1.09 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
package hash
import "fmt"
import (
"github.com/chris-wood/spud/codec"
"crypto/subtle"
)
type HashType uint16
const HashTypeSHA256 HashType = 0x0001
const HashTypeSHA512 HashType = 0x0002
type Hash struct {
hashType HashType
digest []byte
}
type hashError struct {
prob string
}
func (e hashError) Error() string {
return fmt.Sprintf("%s", e.prob)
}
// Constructors
func Create(hashType HashType, value []byte) Hash {
return Hash{hashType, value}
}
func CreateFromTLV(tlv codec.TLV) (Hash, error) {
var result Hash
return result, nil
}
// TLV functions
func (h Hash) Type() uint16 {
return uint16(codec.T_HASH)
}
func (h Hash) TypeString() string {
return "Hash"
}
func (h Hash) Length() uint16 {
return uint16(len(h.digest))
}
func (h Hash) Value() []byte {
return h.digest
}
func (h Hash) Children() []codec.TLV {
return nil
}
func (h Hash) String() string {
return string(h.digest)
}
// Utility functions
func (h Hash) Equals(other Hash) bool {
if h.hashType == other.hashType {
if subtle.ConstantTimeCompare(h.digest, other.digest) == 0 {
return true
}
}
return false
}