𝗕𝗮𝘀𝗲𝟲𝟰 𝗘𝗻𝗰𝗼𝗱𝗶𝗻𝗴: 𝗪𝗵𝗲𝗻 𝘁𝗼 𝗨𝘀𝗲 𝗜𝘁
Base64 turns binary data into ASCII text. Many developers use it wrong. Here is how you use it right.
Every 3 bytes of data become 4 characters. This adds 33% to the file size. You must justify this cost.
Use Base64 for these tasks:
- Small icons in HTML. This saves a network request.
- JWT tokens.
- HTTP Basic Auth.
- Email attachments.
Avoid Base64 for these tasks:
- Large files. Use external files for anything over 5 KB. Large strings slow down your page.
- Security. Base64 is not encryption. Anyone decodes it in one second.
- Hiding keys. API keys and passwords stay plain text. Use environment variables instead.
Quick code tips:
- Browser JS: use btoa() to encode and atob() to decode.
- Node.js: use Buffer.from().
- Python: use the base64 library.
- Linux: use the base64 command.
Use a browser tool for quick tests. It is the fastest way.
Source: https://dev.to/snappy_tools/base64-encoding-what-it-is-when-to-use-it-and-when-not-to-3jcm