-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.go
More file actions
92 lines (79 loc) · 2.64 KB
/
Copy pathbase64.go
File metadata and controls
92 lines (79 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package slice
import "encoding/base64"
// TODO 待测试
// ------------------------------------------------ ---------------------------------------------------------------------
// Base64Encode 把字节数组编码为base64的字节数组
func Base64Encode(byteSlice []byte) []byte {
if len(byteSlice) == 0 {
return nil
}
dest := make([]byte, base64.StdEncoding.EncodedLen(len(byteSlice)))
base64.StdEncoding.Encode(dest, byteSlice)
return dest
}
// Base64EncodeAsString 把字节数组编码为base64字符串
func Base64EncodeAsString(byteSlice []byte) string {
if len(byteSlice) == 0 {
return ""
}
return string(Base64Encode(byteSlice))
}
// ------------------------------------------------ ---------------------------------------------------------------------
// Base64Decode 把Base64编码的字节数组解析为字节数组,如果发生了错误则忽略错误并返回一个空字节数组
func Base64Decode(byteSlice []byte) []byte {
if len(byteSlice) == 0 {
return nil
}
result, err := Base64DecodeE(byteSlice)
if err != nil {
return []byte{}
}
return result
}
// Base64DecodeE 把Base64编码的字节数组解码,如果发生了错误则返回错误
func Base64DecodeE(byteSlice []byte) ([]byte, error) {
if len(byteSlice) == 0 {
return nil, nil
}
dest := make([]byte, base64.StdEncoding.DecodedLen(len(byteSlice)))
_, err := base64.StdEncoding.Decode(dest, byteSlice)
if err != nil {
return nil, err
}
return dest, nil
}
// Base64DecodeAsStringE 把Base64编码的字节数组解码为字符串,如果发生了错误则返回错误
func Base64DecodeAsStringE(byteSlice []byte) (string, error) {
if len(byteSlice) == 0 {
return "", nil
}
bytes, err := Base64DecodeE(byteSlice)
if err != nil {
return "", err
}
return string(bytes), nil
}
// Base64DecodeAsString 把Base64编码的字节数组解码为字符串,如果发生了错误则忽略错误返回空字符串
func Base64DecodeAsString(byteSlice []byte) string {
if len(byteSlice) == 0 {
return ""
}
result, err := Base64DecodeAsStringE(byteSlice)
if err != nil {
return ""
}
return result
}
// ------------------------------------------------- ------------------------------------------------------------------------
// TODO
//// BinaryToAscii 把二进制转为ascii字符串,用于兼容浏览器环境中的btoa
//func BinaryToAscii(binaryBase64String string) string {
//
//}
//
// TODO
//// AsciiToBinary 把ascii转为二进制,用于兼容浏览器环境中的atob
//func AsciiToBinary(asciiString string) []byte {
//
//}
// ------------------------------------------------- ------------------------------------------------------------------------