Apr 152020

ADM

Base64 Encode in Golang

  • 15 April 2020
  • ADM

 

 

 

Base64 Encode in Golang - images/logos/golang.jpg

 

Golang programing language provides built-in suport for base64 encoding. The base64 functions are available into the encoding/base64 package. It provides implementations for both Standard, with or without padding and Filename safe Base64 encoding variant.

Note: if you need an online tool to encode to base64 you can use Base64 Encoder Online tool, also available for files.

Base64 table

Base64 encoding uses a subset of ASCII character set containing the characters: A-Z, a-z, 0-9, +, and /

Index Char Index Char Index Char Index Char
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

Padding

The equal (=) it is used as the padding char. It is used to pad the string to a multiple of four. Due to the format of Base64, you will only see one or two padding chars.

Golang Base64 Encoding

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {

	str := "This is a test!!"

	base64Str := base64.StdEncoding.EncodeToString([]byte(str))

	fmt.Println("String: ", str)
	fmt.Println("base64: ", string(base64Str))
}

Output

String:  This is a test!!
base64:  VGhpcyBpcyBhIHRlc3QhIQ==

To check the decode examples, please check the Base64 Decode Golang tutorial.

 

Golang Base64 URL / Filename safe Encoding

This is a variant of Base64 encoding whose output is safe to be used as filename or URLs. Because + and / characters are part of the standard Base64 alphabet are not URL or Filename safe, therefpre this variant replaces + with minus (-) and / with underscore (_).

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {

	str := "This is a test for + URL + encoding \ base64 ! ? ."

	base64Str := base64.URLEncoding.EncodeToString([]byte(str))

	fmt.Println("String: ", str)
	fmt.Println("Base64: ", string(base64Str))
}

Output

String:  This is a test for + URL + encoding  base64 ! ? .
Base64:  VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA_IC4=

 

Golang Base64 Encoding without Padding

To encode without the padding, you can use the Raw encodings provided by the encoding/base64 Golang package.

The Raw is available for both Standard and URL encoding.

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {

	str := "This is a test for + URL + encoding \ base64 ! ? ."

	base64Str0 := base64.RawStdEncoding.EncodeToString([]byte(str))
	base64Str1 := base64.RawURLEncoding.EncodeToString([]byte(str))

	fmt.Println("String: ", str)
	fmt.Println("Base64 Standard, no padding: ", base64Str0)
	fmt.Println("Base64 URL, no padding: ", base64Str1)
}

Output

String:  This is a test for + URL + encoding  base64 ! ? .
Base64 Standard, no padding:  VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA/IC4
Base64 URL, no padding:  VGhpcyBpcyBhIHRlc3QgZm9yICsgVVJMICsgZW5jb2RpbmcgXCBiYXNlNjQgISA_IC4

 

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build example.go

Assuming that example.go is the name of your file.

Then depending on if you are on Linux or Windows the binary file is created.

To run the application, execute the command.

Linux

$ ./example

Windows

c:\Users\adm\go\tutorials> example.exe

If you want to compile and run the application in one single step, run the following command:

go run example.go

 

 

 

References