github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/lib/crypto/encoding.go (about) 1 package crypto 2 3 import ( 4 "encoding/base64" 5 "encoding/hex" 6 ) 7 8 const ( 9 // KeySize is the size of the encryption key in bytes 10 KeySize = 32 11 // IVSize is the size of the IV in bytes 12 IVSize = 12 13 // LegacyIVSize is the size of the IV in bytes for the legacy encryption scheme 14 LegacyIVSize = 16 15 // AADSize is the size in bytes of the Additional Authenticated Data for the 16 // GCM encryption 17 AADSize = 16 18 ) 19 20 // Hex encode bytes 21 func (c *SCrypto) Hex(src []byte, maxLen int) []byte { 22 dst := make([]byte, hex.EncodedLen(len(src))) 23 hex.Encode(dst, src) 24 if len(dst) > maxLen { 25 // avoid extraneous padding 26 dst = dst[:maxLen] 27 } 28 return dst 29 } 30 31 // Unhex bytes 32 func (c *SCrypto) Unhex(src []byte, maxLen int) []byte { 33 dst := make([]byte, hex.DecodedLen(len(src))) 34 hex.Decode(dst, src) 35 if len(dst) > maxLen { 36 // avoid extraneous padding 37 dst = dst[:maxLen] 38 } 39 return dst 40 } 41 42 // Base64Encode bytes 43 func (c *SCrypto) Base64Encode(src []byte, maxLen int) []byte { 44 dst := make([]byte, base64.StdEncoding.EncodedLen(len(src))) 45 base64.StdEncoding.Encode(dst, src) 46 if len(dst) > maxLen { 47 // avoid extraneous padding 48 dst = dst[:maxLen] 49 } 50 return dst 51 } 52 53 // Base64Decode bytes 54 func (c *SCrypto) Base64Decode(src []byte, maxLen int) []byte { 55 dst := make([]byte, base64.StdEncoding.DecodedLen(len(src))) 56 base64.StdEncoding.Decode(dst, src) 57 if len(dst) > maxLen { 58 // avoid extraneous padding 59 dst = dst[:maxLen] 60 } 61 return dst 62 }