github.com/gogf/gf@v1.16.9/encoding/gbase64/gbase64.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
     6  
     7  // Package gbase64 provides useful API for BASE64 encoding/decoding algorithm.
     8  package gbase64
     9  
    10  import (
    11  	"encoding/base64"
    12  	"io/ioutil"
    13  )
    14  
    15  // Encode encodes bytes with BASE64 algorithm.
    16  func Encode(src []byte) []byte {
    17  	dst := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
    18  	base64.StdEncoding.Encode(dst, src)
    19  	return dst
    20  }
    21  
    22  // EncodeString encodes string with BASE64 algorithm.
    23  func EncodeString(src string) string {
    24  	return EncodeToString([]byte(src))
    25  }
    26  
    27  // EncodeToString encodes bytes to string with BASE64 algorithm.
    28  func EncodeToString(src []byte) string {
    29  	return string(Encode(src))
    30  }
    31  
    32  // EncryptFile encodes file content of <path> using BASE64 algorithms.
    33  func EncodeFile(path string) ([]byte, error) {
    34  	content, err := ioutil.ReadFile(path)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return Encode(content), nil
    39  }
    40  
    41  // MustEncodeFile encodes file content of <path> using BASE64 algorithms.
    42  // It panics if any error occurs.
    43  func MustEncodeFile(path string) []byte {
    44  	result, err := EncodeFile(path)
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	return result
    49  }
    50  
    51  // EncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
    52  func EncodeFileToString(path string) (string, error) {
    53  	content, err := EncodeFile(path)
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  	return string(content), nil
    58  }
    59  
    60  // MustEncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
    61  // It panics if any error occurs.
    62  func MustEncodeFileToString(path string) string {
    63  	result, err := EncodeFileToString(path)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  	return result
    68  }
    69  
    70  // Decode decodes bytes with BASE64 algorithm.
    71  func Decode(data []byte) ([]byte, error) {
    72  	src := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
    73  	n, err := base64.StdEncoding.Decode(src, data)
    74  	return src[:n], err
    75  }
    76  
    77  // MustDecode decodes bytes with BASE64 algorithm.
    78  // It panics if any error occurs.
    79  func MustDecode(data []byte) []byte {
    80  	result, err := Decode(data)
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  	return result
    85  }
    86  
    87  // DecodeString decodes string with BASE64 algorithm.
    88  func DecodeString(data string) ([]byte, error) {
    89  	return Decode([]byte(data))
    90  }
    91  
    92  // MustDecodeString decodes string with BASE64 algorithm.
    93  // It panics if any error occurs.
    94  func MustDecodeString(data string) []byte {
    95  	result, err := DecodeString(data)
    96  	if err != nil {
    97  		panic(err)
    98  	}
    99  	return result
   100  }
   101  
   102  // DecodeString decodes string with BASE64 algorithm.
   103  func DecodeToString(data string) (string, error) {
   104  	b, err := DecodeString(data)
   105  	return string(b), err
   106  }
   107  
   108  // MustDecodeToString decodes string with BASE64 algorithm.
   109  // It panics if any error occurs.
   110  func MustDecodeToString(data string) string {
   111  	result, err := DecodeToString(data)
   112  	if err != nil {
   113  		panic(err)
   114  	}
   115  	return result
   116  }