Data Encoding Explained: Base64, URLs, Hashes
Base64, URL encoding and hashing compared: how each one works, what it is meant for, and which to pick for your data.
Why Do We Need Data Encoding?
In computer systems, data needs to be transmitted and stored across different environments. But not all environments can safely handle all types of bytes. For example:
- Email protocols (SMTP) originally only supported 7-bit ASCII characters
- Certain characters in URLs (like
&,=,#) have special meanings - Binary files can't be directly embedded in text formats (like JSON, XML)
The purpose of data encoding is to convert data into a format that the target environment can safely accept. Understanding the principles and use cases of different encoding methods is key to avoiding data corruption and security vulnerabilities.
Base64 Encoding
How It Works
Base64 converts every 3 bytes (24 bits) of binary data into 4 printable ASCII characters (6 bits each). The encoding table contains 64 characters: A-Z, a-z, 0-9, +, /, plus = for padding.
Original: "Hello" (5 bytes = 40 bits)
Base64: "SGVsbG8=" (8 characters)
Key Properties
- Not encryption: Base64 can be easily decoded — it provides no security
- Size increase: Encoded size is about 4/3 of the original (~33% larger)
- Reversible: Encoding and decoding are fully reversible with no data loss
Common Use Cases
- Embedding binary data in JSON/XML: Converting images to Base64 strings inline in HTML
- HTTP Basic Authentication:
Authorization: Basic <base64(username:password)> - Data URI Scheme:
data:image/png;base64,iVBOR...embedding images directly in HTML - Email attachments: MIME protocol uses Base64 to encode binary attachments
Common Misconceptions
Myth: Base64 can protect sensitive data
// Wrong: "encrypting" a password with Base64
const encoded = btoa('mySecretPassword'); // "bXlTZWNyZXRQYXNzd29yZA=="
// Anyone can decode it
atob(encoded); // "mySecretPassword"
Base64 is encoding, not encryption. If you need to protect sensitive data, use encryption algorithms like AES.
URL Encoding (Percent-Encoding)
How It Works
URL encoding converts special characters to %XX format, where XX is the hexadecimal representation of the character's UTF-8 encoding. For example:
Space → %20
Chinese "你" → %E4%BD%A0
& → %26
# → %23
Encoding Rules
Per RFC 3986, the following characters do not need encoding (unreserved characters):
Letters: A-Z a-z
Digits: 0-9
Special: - _ . ~
All other characters must be encoded. Notable cases:
| Character | Needs Encoding? | Reason |
|---|---|---|
| Space | Yes | Ambiguous in URLs |
| Chinese | Yes | Non-ASCII character |
& |
Yes (in param values) | Separates parameters in URLs |
= |
Yes (in param values) | Separates key-value in URLs |
/ |
Depends | Path separator usually not encoded |
Common Use Cases
- URL query parameters:
search?q=hello%20world&type=article - Form data:
application/x-www-form-urlencodedformat - File paths: Filenames with special characters need encoding
encodeURI vs encodeURIComponent
// encodeURI: encodes a full URL, preserving protocol and structural characters
encodeURI('https://example.com/search?q=hello world&lang=zh')
// "https://example.com/search?q=hello%20world&lang=zh"
// encodeURIComponent: encodes a URL component, encoding all special characters
encodeURIComponent('hello world&lang=zh')
// "hello%20world%26lang%3Dzh"
Rule: Use encodeURI for full URLs, encodeURIComponent for individual parameter values.
Hash Algorithms
How They Work
Hash functions convert arbitrary-length input into fixed-length output (digest). Key properties:
- Deterministic: Same input always produces same output
- Irreversible: Cannot recover original data from hash
- Collision-resistant: Hard to find two different inputs with the same hash
- Avalanche effect: Tiny input changes cause massive output changes
Algorithm Comparison
| Algorithm | Output Length | Speed | Security | Recommended For |
|---|---|---|---|---|
| MD5 | 128-bit | Fastest | Broken | File checksums (non-security) |
| SHA-1 | 160-bit | Fast | Broken | Not recommended for security |
| SHA-256 | 256-bit | Medium | Secure | General hashing, digital signatures |
| SHA-512 | 512-bit | Slower | Very secure | High-security scenarios |
| bcrypt | Variable | Slow | Very secure | Password storage |
HMAC: Keyed Hashing
HMAC (Hash-based Message Authentication Code) adds a key to hashing, used to verify data integrity and origin:
HMAC = Hash((Key XOR opad) || Hash((Key XOR ipad) || message))
Use cases:
- API request signing (AWS Signature, WeChat Pay callback verification)
- Message integrity verification
- Token generation (JWT uses HMAC-SHA256)
Encoding vs Hashing vs Encryption
| Property | Encoding (Base64/URL) | Hashing (SHA-256) | Encryption (AES) |
|---|---|---|---|
| Reversible | Fully reversible | Irreversible | Reversible (with key) |
| Purpose | Format conversion | Integrity verification | Confidentiality protection |
| Output Length | Input-dependent | Fixed length | Input-dependent |
| Key Required | No | No (HMAC needs one) | Yes |
Practical Guidelines
- Transmit binary data to text environments → Use Base64
- Construct URL parameters → Use
encodeURIComponent - Verify file integrity → Use SHA-256 hash
- API signature verification → Use HMAC-SHA256
- Store passwords → Use bcrypt or Argon2, never MD5/SHA
Online Tools
- Base64 Encoder — Encode and decode text and files in Base64
- URL Encoder — Handle URL and component encoding/decoding
- Hash / HMAC Calculator — Calculate SHA hashes and HMAC signatures
Understanding the essence of data encoding helps you make the right technical choices in development, avoiding data corruption and security vulnerabilities.