github.com/0chain/gosdk@v1.17.11/zcnbridge/crypto/hash.go (about) 1 // Methods and types to support cryptographic operations. 2 package crypto 3 4 import ( 5 "encoding/hex" 6 7 "golang.org/x/crypto/sha3" 8 ) 9 10 const HashLength = 32 11 12 type HashBytes [HashLength]byte 13 14 // Hash computes hash of the given data using RawHash and returns result as hex decoded string. 15 func Hash(data interface{}) string { 16 return hex.EncodeToString(RawHash(data)) 17 } 18 19 // RawHash computes SHA3-256 hash depending on data type and returns the hash bytes. 20 // 21 // RawHash panics if data type is unknown. 22 // 23 // Known types: 24 // 25 // - []byte 26 // 27 // - HashBytes 28 // 29 // - string 30 func RawHash(data interface{}) []byte { 31 var databuf []byte 32 switch dataImpl := data.(type) { 33 case []byte: 34 databuf = dataImpl 35 case HashBytes: 36 databuf = dataImpl[:] 37 case string: 38 databuf = []byte(dataImpl) 39 default: 40 panic("unknown type") 41 } 42 hash := sha3.New256() 43 hash.Write(databuf) 44 var buf []byte 45 return hash.Sum(buf) 46 }