Every few days I need to Base64 a file, hash a download, or turn an epoch value into a date, and every few days I search for the exact same one-liner. So I finally wrote them down in one place. Copy what you need.
Base64
| Where | Encode | Decode |
|---|---|---|
| Linux / macOS | base64 file.png |
base64 -d encoded.txt (-D on older macOS) |
| PowerShell | [Convert]::ToBase64String([IO.File]::ReadAllBytes('file.png')) |
[Convert]::FromBase64String($s) |
| Python | base64.b64encode(data) |
base64.b64decode(s) |
| Browser JS | btoa(str) |
atob(s) |
| Node.js | Buffer.from(str).toString('base64') |
Buffer.from(s, 'base64') |
| PHP | base64_encode($data) |
base64_decode($s) |
Gotcha: btoa only accepts Latin-1. Text with accents or emoji needs a TextEncoder step first, or it throws.
URL encoding
| Language | Encode one value | Watch out |
|---|---|---|
| JavaScript | encodeURIComponent(value) |
encodeURI is for a whole address |
| Python | urllib.parse.quote(value, safe='') |
quote_plus turns spaces into +
|
| PHP | rawurlencode($value) |
urlencode uses + for spaces |
| Java | URLEncoder.encode(value, "UTF-8") |
produces +; use %20 inside a path |
| C# | Uri.EscapeDataString(value) |
HttpUtility.UrlEncode uses +
|
Gotcha: if you see %2520, the value was encoded twice. Decode once, encode once.
File hashes (checking a download)
| System | SHA-256 | MD5 |
|---|---|---|
| Windows cmd | certutil -hashfile file.iso SHA256 |
certutil -hashfile file.iso MD5 |
| PowerShell | Get-FileHash file.iso -Algorithm SHA256 |
Get-FileHash file.iso -Algorithm MD5 |
| Linux | sha256sum file.iso |
md5sum file.iso |
| macOS | shasum -a 256 file.iso |
md5 file.iso |
| Python | hashlib.sha256(data).hexdigest() |
hashlib.md5(data).hexdigest() |
The result is identical everywhere: a hash depends only on the bytes, never on the tool.
UUIDs
| Platform | One-liner |
|---|---|
| JavaScript | crypto.randomUUID() |
| Python | import uuid; uuid.uuid4() |
| Java | UUID.randomUUID() |
| C# |
Guid.NewGuid() (Guid.CreateVersion7() on .NET 9) |
| PostgreSQL |
gen_random_uuid() (uuidv7() from PostgreSQL 18) |
| MySQL |
UUID() (careful: this is a time-based v1) |
| Shell | uuidgen |
Gotcha: for database primary keys, prefer v7. It starts with a millisecond timestamp, so new rows land at the end of the index instead of scattering through it.
Unix timestamps
| Where | Now | Timestamp to date |
|---|---|---|
| JavaScript | Math.floor(Date.now() / 1000) |
new Date(ts * 1000) |
| Python | int(time.time()) |
datetime.fromtimestamp(ts, tz=timezone.utc) |
| PHP | time() |
date('Y-m-d H:i:s', $ts) |
| MySQL | UNIX_TIMESTAMP() |
FROM_UNIXTIME(ts) |
| PostgreSQL | EXTRACT(EPOCH FROM now()) |
to_timestamp(ts) |
| Shell | date +%s |
date -d @1700000000 (date -r 1700000000 on macOS) |
| Excel | – |
=A1/86400 + DATE(1970,1,1) (UTC) |
Gotcha: 10 digits is seconds, 13 digits is milliseconds. A date in 1970 means you passed milliseconds where seconds were expected, or the other way round.
Discord bonus: <t:1700000000:R> renders as a relative time in every reader's own time zone.
When I don't have a terminal handy, I use the browser versions of these on LogicUtil, a set of free tools that run entirely client-side (nothing is uploaded, no sign-up). Disclosure: I built it. The same tables also live in a GitHub repo: saman-webdev/dev-one-liners. If one of the one-liners above is wrong for your platform, tell me in the comments and I'll fix it.
Top comments (0)