github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/common/bytes.go (about) 1 package common 2 3 import ( 4 "encoding/base32" 5 "encoding/hex" 6 ) 7 8 const Alphabet = `123456789abcdefghjkmnpqrstuvwxyz` 9 10 var encoding = base32.NewEncoding(Alphabet) 11 12 func ToBase32(hash []byte) string { 13 return encoding.EncodeToString(hash) 14 } 15 16 func FromBase32(std32 string) ([]byte, error) { 17 return encoding.DecodeString(std32) 18 } 19 20 // Bytes2Hex returns the hexadecimal encoding of d. 21 func Bytes2Hex(d []byte) string { 22 return hex.EncodeToString(d) 23 } 24 25 // Hex2Bytes returns the bytes represented by the hexadecimal string str. 26 func Hex2Bytes(str string) ([]byte, error) { 27 h, err := hex.DecodeString(str) 28 return h, err 29 }