github.com/zhongdalu/gf@v1.0.0/g/encoding/gbase64/gbase64.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // Package gbase64 provides useful API for BASE64 encoding/decoding algorithm.
     8  package gbase64
     9  
    10  import (
    11  	"encoding/base64"
    12  )
    13  
    14  // Encode encodes bytes with BASE64 algorithm.
    15  func Encode(src []byte) []byte {
    16  	dst := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
    17  	base64.StdEncoding.Encode(dst, src)
    18  	return dst
    19  }
    20  
    21  // Decode decodes bytes with BASE64 algorithm.
    22  func Decode(dst []byte) ([]byte, error) {
    23  	src := make([]byte, base64.StdEncoding.DecodedLen(len(dst)))
    24  	n, err := base64.StdEncoding.Decode(src, dst)
    25  	return src[:n], err
    26  }
    27  
    28  // EncodeString encodes bytes with BASE64 algorithm.
    29  func EncodeString(src []byte) string {
    30  	return string(Encode(src))
    31  }
    32  
    33  // DecodeString decodes string with BASE64 algorithm.
    34  func DecodeString(str string) ([]byte, error) {
    35  	return Decode([]byte(str))
    36  }