01 |
package main |
02 |
03 |
import ( |
04 |
"encoding/base64" |
05 |
"fmt" |
06 |
) |
07 |
08 |
const ( |
09 |
base64Table =
"123QRSTUabcdVWXYZHijKLAWDCABDstEFGuvwxyzGHIJklmnopqr234560178912" |
10 |
) |
11 |
12 |
var coder = base64.NewEncoding(base64Table) |
13 |
14 |
func base64Encode(src []byte) []byte { |
15 |
return
[]byte(coder.EncodeToString(src)) |
16 |
} |
17 |
18 |
func base64Decode(src []byte) ([]byte, error) { |
19 |
return
coder.DecodeString(string(src)) |
20 |
} |
21 |
22 |
func main() { |
23 |
// encode
|
24 |
hello :=
"hello world" |
25 |
debyte := base64Encode([]byte(hello)) |
26 |
27 |
// decode
|
28 |
enbyte, err := base64Decode(debyte) |
29 |
if
err != nil { |
30 |
fmt.Println(err.Error()) |
31 |
} |
32 |
33 |
if
hello != string(enbyte) { |
34 |
fmt.Println( "hello is not equal to enbyte" ) |
35 |
} |
36 |
37 |
fmt.Println(string(enbyte)) |
38 |
} |