github.com/tickstep/library-go@v0.1.1/crypto/bs64.go (about) 1 package crypto 2 3 import ( 4 "bytes" 5 "encoding/base64" 6 "io/ioutil" 7 ) 8 9 // Base64Encode base64加密 10 func Base64Encode(raw []byte) []byte { 11 var encoded bytes.Buffer 12 encoder := base64.NewEncoder(base64.StdEncoding, &encoded) 13 encoder.Write(raw) 14 encoder.Close() 15 return encoded.Bytes() 16 } 17 18 // Base64EncodeStr base64字符串加密 19 func Base64EncodeStr(raw string) string { 20 return string(Base64Encode([]byte(raw))) 21 } 22 23 // Base64Decode base64解密 24 func Base64Decode(raw []byte) []byte { 25 var buf bytes.Buffer 26 buf.Write(raw) 27 decoder := base64.NewDecoder(base64.StdEncoding, &buf) 28 decoded, _ := ioutil.ReadAll(decoder) 29 return decoded 30 } 31 32 // Base64DecodeStr base64字符串解密 33 func Base64DecodeStr(bs64str string) string { 34 return string(Base64Decode([]byte(bs64str))) 35 }