logo

Base64

Shell

# encode
$ echo "admin" | base64
YWRtaW4K

# decode
$ echo "YWRtaW4K" | base64 -d
admin

The first base64 encoded value has an extra \n at the end of the output where as the other 2 do not.

$ echo "hello world" | base64
aGVsbG8gd29ybGQK

$ echo -n "hello world" | base64
aGVsbG8gd29ybGQ=

$ printf "hello world" | base64
aGVsbG8gd29ybGQ=

Go

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {
	msg := "Hello, 世界"
	encoded := base64.StdEncoding.EncodeToString([]byte(msg))
	fmt.Println(encoded)
	decoded, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		fmt.Println("decode error:", err)
		return
	}
	fmt.Println(string(decoded))
}

Node.js

Encoding

import { Buffer } from 'buffer';
var b = new Buffer('JavaScript');
var s = b.toString('base64');
// SmF2YVNjcmlwdA==

Decoding

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64');
var s = b.toString();
// JavaScript

To Hex

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64');
var s = b.toString('hex');
// 4a617661536372697074

From Hex

var b = new Buffer('4a617661536372697074', 'hex');
var s = b.toString('utf8');
// JavaScript