-
Notifications
You must be signed in to change notification settings - Fork 26
/
secure_value.go
66 lines (54 loc) · 1.47 KB
/
secure_value.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
package util
import (
"encoding/hex"
"strings"
"github.com/0chain/gosdk/core/encryption"
)
/*Hashable - anything that can provide it's hash */
type Hashable interface {
GetHash() string
GetHashBytes() []byte
}
/*Serializable interface */
type Serializable interface {
Encode() []byte
Decode([]byte) error
}
/*HashStringToBytes - convert a hex hash string to bytes */
func HashStringToBytes(hash string) []byte {
hashBytes, err := hex.DecodeString(hash)
if err != nil {
return nil
}
return hashBytes
}
/*SecureSerializableValueI an interface that makes a serializable value secure with hashing */
type SecureSerializableValueI interface {
Serializable
Hashable
}
/*SecureSerializableValue - a proxy persisted value that just tracks the encoded bytes of a persisted value */
type SecureSerializableValue struct {
Buffer []byte
}
/*GetHash - implement interface */
func (spv *SecureSerializableValue) GetHash() string {
return ToHex(spv.GetHashBytes())
}
/*ToHex - converts a byte array to hex encoding with upper case */
func ToHex(buf []byte) string {
return strings.ToUpper(hex.EncodeToString(buf))
}
/*GetHashBytes - implement interface */
func (spv *SecureSerializableValue) GetHashBytes() []byte {
return encryption.RawHash(spv.Buffer)
}
/*Encode - implement interface */
func (spv *SecureSerializableValue) Encode() []byte {
return spv.Buffer
}
/*Decode - implement interface */
func (spv *SecureSerializableValue) Decode(buf []byte) error {
spv.Buffer = buf
return nil
}