2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-07-02 16:56:10 +08:00
|
|
|
// Package gbase64 provides useful API for BASE64 encoding/decoding algorithm.
|
2017-11-23 10:21:28 +08:00
|
|
|
package gbase64
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"encoding/base64"
|
2023-08-01 21:15:28 +08:00
|
|
|
"os"
|
2021-12-21 22:59:14 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2017-11-23 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
2019-07-02 16:56:10 +08:00
|
|
|
// Encode encodes bytes with BASE64 algorithm.
|
|
|
|
func Encode(src []byte) []byte {
|
|
|
|
dst := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
|
|
|
|
base64.StdEncoding.Encode(dst, src)
|
|
|
|
return dst
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:19:18 +08:00
|
|
|
// EncodeString encodes string with BASE64 algorithm.
|
|
|
|
func EncodeString(src string) string {
|
|
|
|
return EncodeToString([]byte(src))
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeToString encodes bytes to string with BASE64 algorithm.
|
|
|
|
func EncodeToString(src []byte) string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(Encode(src))
|
2019-07-02 16:56:10 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
// EncodeFile encodes file content of `path` using BASE64 algorithms.
|
2019-10-31 14:42:01 +08:00
|
|
|
func EncodeFile(path string) ([]byte, error) {
|
2023-08-01 21:15:28 +08:00
|
|
|
content, err := os.ReadFile(path)
|
2019-10-31 14:42:01 +08:00
|
|
|
if err != nil {
|
2023-08-01 21:15:28 +08:00
|
|
|
err = gerror.Wrapf(err, `os.ReadFile failed for filename "%s"`, path)
|
2019-10-31 14:42:01 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Encode(content), nil
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// MustEncodeFile encodes file content of `path` using BASE64 algorithms.
|
2019-12-12 11:40:23 +08:00
|
|
|
// It panics if any error occurs.
|
|
|
|
func MustEncodeFile(path string) []byte {
|
|
|
|
result, err := EncodeFile(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// EncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
|
2019-10-31 14:42:01 +08:00
|
|
|
func EncodeFileToString(path string) (string, error) {
|
|
|
|
content, err := EncodeFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(content), nil
|
2019-10-31 14:42:01 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// MustEncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
|
2019-12-12 11:40:23 +08:00
|
|
|
// It panics if any error occurs.
|
|
|
|
func MustEncodeFileToString(path string) string {
|
|
|
|
result, err := EncodeFileToString(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode decodes bytes with BASE64 algorithm.
|
|
|
|
func Decode(data []byte) ([]byte, error) {
|
2021-12-21 22:59:14 +08:00
|
|
|
var (
|
|
|
|
src = make([]byte, base64.StdEncoding.DecodedLen(len(data)))
|
|
|
|
n, err = base64.StdEncoding.Decode(src, data)
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err = gerror.Wrap(err, `base64.StdEncoding.Decode failed`)
|
|
|
|
}
|
2019-12-12 11:40:23 +08:00
|
|
|
return src[:n], err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustDecode decodes bytes with BASE64 algorithm.
|
|
|
|
// It panics if any error occurs.
|
|
|
|
func MustDecode(data []byte) []byte {
|
|
|
|
result, err := Decode(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:56:10 +08:00
|
|
|
// DecodeString decodes string with BASE64 algorithm.
|
2019-12-12 11:40:23 +08:00
|
|
|
func DecodeString(data string) ([]byte, error) {
|
|
|
|
return Decode([]byte(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustDecodeString decodes string with BASE64 algorithm.
|
|
|
|
// It panics if any error occurs.
|
|
|
|
func MustDecodeString(data string) []byte {
|
|
|
|
result, err := DecodeString(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-09-24 20:19:18 +08:00
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
// DecodeToString decodes string with BASE64 algorithm.
|
2019-12-12 11:40:23 +08:00
|
|
|
func DecodeToString(data string) (string, error) {
|
|
|
|
b, err := DecodeString(data)
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(b), err
|
2019-09-24 20:19:18 +08:00
|
|
|
}
|
2019-12-12 11:40:23 +08:00
|
|
|
|
|
|
|
// MustDecodeToString decodes string with BASE64 algorithm.
|
|
|
|
// It panics if any error occurs.
|
|
|
|
func MustDecodeToString(data string) string {
|
|
|
|
result, err := DecodeToString(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|