github.com/0chain/gosdk@v1.17.11/core/util/secure_value.go (about) 1 package util 2 3 import ( 4 "encoding/hex" 5 "strings" 6 7 "github.com/0chain/gosdk/core/encryption" 8 ) 9 10 // Hashable anything that can provide it's hash 11 type Hashable interface { 12 // GetHash get the hash of the object 13 GetHash() string 14 15 // GetHashBytes get the hash of the object as bytes 16 GetHashBytes() []byte 17 18 // Write write the bytes to the hash 19 Write(b []byte) (int, error) 20 } 21 22 /*Serializable interface */ 23 type Serializable interface { 24 Encode() []byte 25 Decode([]byte) error 26 } 27 28 /*HashStringToBytes - convert a hex hash string to bytes */ 29 func HashStringToBytes(hash string) []byte { 30 hashBytes, err := hex.DecodeString(hash) 31 if err != nil { 32 return nil 33 } 34 return hashBytes 35 } 36 37 /*SecureSerializableValueI an interface that makes a serializable value secure with hashing */ 38 type SecureSerializableValueI interface { 39 Serializable 40 Hashable 41 } 42 43 /*SecureSerializableValue - a proxy persisted value that just tracks the encoded bytes of a persisted value */ 44 type SecureSerializableValue struct { 45 Buffer []byte 46 } 47 48 /*GetHash - implement interface */ 49 func (spv *SecureSerializableValue) GetHash() string { 50 return ToHex(spv.GetHashBytes()) 51 } 52 53 /*ToHex - converts a byte array to hex encoding with upper case */ 54 func ToHex(buf []byte) string { 55 return strings.ToUpper(hex.EncodeToString(buf)) 56 } 57 58 /*GetHashBytes - implement interface */ 59 func (spv *SecureSerializableValue) GetHashBytes() []byte { 60 return encryption.RawHash(spv.Buffer) 61 } 62 63 /*Encode - implement interface */ 64 func (spv *SecureSerializableValue) Encode() []byte { 65 return spv.Buffer 66 } 67 68 /*Decode - implement interface */ 69 func (spv *SecureSerializableValue) Decode(buf []byte) error { 70 spv.Buffer = buf 71 return nil 72 }