mirror of
https://gitee.com/johng/gf.git
synced 2024-12-04 21:28:22 +08:00
37 lines
1.0 KiB
Go
37 lines
1.0 KiB
Go
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// 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,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
// Package gbase64 provides useful API for BASE64 encoding/decoding algorithm.
|
|
package gbase64
|
|
|
|
import (
|
|
"encoding/base64"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// Decode decodes bytes with BASE64 algorithm.
|
|
func Decode(dst []byte) ([]byte, error) {
|
|
src := make([]byte, base64.StdEncoding.DecodedLen(len(dst)))
|
|
n, err := base64.StdEncoding.Decode(src, dst)
|
|
return src[:n], err
|
|
}
|
|
|
|
// EncodeString encodes bytes with BASE64 algorithm.
|
|
func EncodeString(src []byte) string {
|
|
return string(Encode(src))
|
|
}
|
|
|
|
// DecodeString decodes string with BASE64 algorithm.
|
|
func DecodeString(str string) ([]byte, error) {
|
|
return Decode([]byte(str))
|
|
}
|