base64 encoding via CLI

Simply use base64 and base64 -d:

zsh> echo -n "username:password"|base64
dXNlcm5hbWU6cGFzc3dvcmQ=

Note: the -n option prevents echo to output a trailing newline (you do not want to encode \n).

zsh> echo dXNlcm5hbWU6cGFzc3dvcmQ=|base64 -d
username:password

base64 encoding is widely used in the web. It is mainly used to carry binary formatted data across channels that only support text content (e.g. HTTP requests or e-mail attachments). In the context of data-exchange through APIs, it is broadly used in the authentication process to include your credentials in the headers:

{
    "Authorization": "Basic <Base64Encoded(username:password)>"
}

Using the Curl Request command:

curl https://your-url
    -H "Authorization: Bearer {token}"

The token being most often your base64 encoded and colon separated credentials (or any other specified credentials).

Using Python

Base64 encoding/decoding with Python:

python> import base64
python> encoded = base64.b64encode(b"username:password")
python> print(encoded)
b'dXNlcm5hbWU6cGFzc3dvcmQ='

python> decoded = base64.b64decode(b'dXNlcm5hbWU6cGFzc3dvcmQ=')
python> print(decoded)
b'username:password'

Notes:

  • With the aforementioned python code, you will then have to decode the b”string” to string. This can be done using the .decode() method:
b'your-string'.decode()
  • If you are on VSC (Visual Studio Code) you can then encode/decode on the fly using an extension e.g. vscode-base64.

Leave a Reply

Your email address will not be published. Required fields are marked *