enc.go (969B)
1 package hashenc 2 3 import ( 4 "encoding/base32" 5 "encoding/base64" 6 "encoding/hex" 7 ) 8 9 // Encoder defines an interface for encoding binary data. 10 type Encoder interface { 11 // Encode encodes the data at src into dst 12 Encode(dst []byte, src []byte) 13 14 // EncodedLen returns the encoded length for input data of supplied length 15 EncodedLen(int) int 16 } 17 18 // Base32 returns a new base32 Encoder (StdEncoding, no padding). 19 func Base32() Encoder { 20 return base32.StdEncoding.WithPadding(base64.NoPadding) 21 } 22 23 // Base64 returns a new base64 Encoder (URLEncoding, no padding). 24 func Base64() Encoder { 25 return base64.URLEncoding.WithPadding(base64.NoPadding) 26 } 27 28 // Hex returns a new hex Encoder. 29 func Hex() Encoder { 30 return &hexEncoder{} 31 } 32 33 // hexEncoder simply provides an empty receiver to satisfy Encoder. 34 type hexEncoder struct{} 35 36 func (*hexEncoder) Encode(dst []byte, src []byte) { 37 hex.Encode(dst, src) 38 } 39 40 func (*hexEncoder) EncodedLen(len int) int { 41 return hex.EncodedLen(len) 42 }