go: base64 decoding

Here is an example how to decode base64 encoded strings https://play.golang.org/p/iojjUKwvZvn
It uses encoding packages encoding/base64 and encoding/hex .

package main

import (
	"fmt"
	"encoding/base64"
	"encoding/hex"
)

func isAscii7printable(bytes []byte)(bool){
   for _, b := range bytes {
   	if b < 32 || b > 127 {
		return false
	}
   }
   return true
}

func main() {
	b64 := "QUJD"  //ABC
	b64  = "QUJD-" //illegal base64 data at input byte 4
	b64  = "QUIK"  //AB\n
	
	bytes, err := base64.StdEncoding.DecodeString(b64)
	if err != nil {
	   fmt.Println("Input string `", b64, "`has an error: ", err.Error())
	   return
	}
        hexBytes := make([]byte, hex.EncodedLen(len(bytes)))
	hex.Encode(hexBytes, bytes)
	
	fmt.Println("Decoding base64 encoded strings")
	fmt.Println("===============================")
	fmt.Printf("Base64: %s\n", b64)
	fmt.Printf("Length: %d bytes; %d bits\n", len(bytes), 8*len(bytes))
	fmt.Printf("Hex   : %s \n", hexBytes)	

	if isAscii7printable(bytes) {
		fmt.Println("String: "+string(bytes))
	} else {
		fmt.Println("There are non ASCII7 characters.")
	}
}

Output could be:
Decoding base64 encoded strings
===============================
Base64: QUIK
Length: 3 bytes; 24 bits
Hex : 41420a
There are non ASCII7 characters.

Or after commenting two b64 lines:
Decoding base64 encoded strings
===============================
Base64: QUJD
Length: 3 bytes; 24 bits
Hex : 414243
String: ABC

For decoding non printable characters is good to use hex.Dump, see https://play.golang.org/p/f6fQpbqeHo8

package main

import (
	"fmt"
	"encoding/base64"
	"encoding/hex"
)

func b64print(b64 string) {
	bytes, err := base64.StdEncoding.DecodeString(b64)
	fmt.Printf("Base64 : %s\n", b64)
	if err != nil {
	   fmt.Printf("Input string '%s' has an error: %s\n\n", b64, err.Error())
	   return
	}
	fmt.Printf("Decoded: length: %d bytes ~ %d bits, `hexdump -C` format:\n", len(bytes), 8*len(bytes))
	fmt.Printf("%s\n", hex.Dump(bytes))	
}

func main() {
	fmt.Println("Decoding base64 encoded strings")
	fmt.Printf("===============================\n\n")

        b64print("wrong")
	b64print("SGVsbG8gV29ybGQh")
	b64print("VGFsbCByZWQgdHJlZSBvbiB0aGUgcml2ZXIgYmFuay4=")
	b64print("QSBsYXp5IGZveCBqdW1wcyBvdmVyIHF1aWNrIGJyb3duIGRvZyEgRW1haWw6dTEwQG15LmV4YW1wbGUub3JnIA==")
}

Here is an output:
Decoding base64 encoded strings
===============================

Base64 : wrong
Input string 'wrong' has an error: illegal base64 data at input byte 4

Base64 : SGVsbG8gV29ybGQh
Decoded: length: 12 bytes ~ 96 bits, `hexdump -C` format:
00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 |Hello World!|

Base64 : VGFsbCByZWQgdHJlZSBvbiB0aGUgcml2ZXIgYmFuay4=
Decoded: length: 32 bytes ~ 256 bits, `hexdump -C` format:
00000000 54 61 6c 6c 20 72 65 64 20 74 72 65 65 20 6f 6e |Tall red tree on|
00000010 20 74 68 65 20 72 69 76 65 72 20 62 61 6e 6b 2e | the river bank.|

Base64 : QSBsYXp5IGZveCBqdW1wcyBvdmVyIHF1aWNrIGJyb3duIGRvZyEgRW1haWw6dTEwQG15LmV4YW1wbGUub3JnIA==
Decoded: length: 64 bytes ~ 512 bits, `hexdump -C` format:
00000000 41 20 6c 61 7a 79 20 66 6f 78 20 6a 75 6d 70 73 |A lazy fox jumps|
00000010 20 6f 76 65 72 20 71 75 69 63 6b 20 62 72 6f 77 | over quick brow|
00000020 6e 20 64 6f 67 21 20 45 6d 61 69 6c 3a 75 31 30 |n dog! Email:u10|
00000030 40 6d 79 2e 65 78 61 6d 70 6c 65 2e 6f 72 67 20 |@my.example.org |

This entry was posted in golang, workday. Bookmark the permalink.

Leave a Reply