How to use the MD5 and Base64 commands in Linux?

Illustration of a Linux terminal showing MD5 hash and Base64 encoded output lines

Two commands cover most checksum and encoding work on Linux: md5sum produces a fixed fingerprint of a file, and base64 converts data into plain ASCII text so it can travel through channels that only accept text. They look similar in a terminal but do opposite jobs. The sections below show the difference through hashes, verified downloads, and decoded Base64 output you can reproduce as-is.

MD5 vs Base64: What Each Command Actually Does

MD5 (Message-Digest algorithm 5) is a hash function. Feed it any input and it returns a 128-bit fingerprint conventionally rendered as 32 hexadecimal characters. Identical content always reproduces that string exactly.

Base64 is not encryption or hashing at all. It is an encoding scheme that represents binary data using 64 ASCII characters, so it is reversible by design.

Anyone with the encoded string can decode it back to the exact original bytes. Safe transport through text-only channels like email bodies and JSON fields is the point, not secrecy.

Propertymd5sumbase64
PurposeIntegrity check via one-way hashReversible text encoding of binary data
Output sizeAlways 32 hex characters (128 bits)Roughly 4/3 of the input size
ReversibleNoYes, with base64 -d
Detects tamperingYes, when you compare against a trusted hashNo
Typical useVerifying downloads and package integrityEmbedding files or credentials in text formats

One honest caveat about MD5: collision attacks against it have been practical since the mid-2000s, so it is retired from security roles like password storage and digital signatures. For accidental-corruption checks on downloaded ISOs and archives, it still works, though SHA-256 (via sha256sum) is the current recommendation for anything an attacker could influence.

Create an MD5 Checksum with md5sum

Both md5sum and base64 are part of GNU coreutils, so they ship with every mainstream distribution and nothing needs installing.

Create a test file and generate its checksum. The hash depends only on the file contents, never the name or timestamp, so renaming a file does not change its MD5 value:

echo "Hi this is a test file from LinuxForDevices" > example.txt
md5sum example.txt
9614a183699d93ca7d0409699f384767  example.txt

The long string on the left is the digest and the filename follows. Running md5sum again on identical content reproduces exactly this line, which is what makes comparison possible across machines.

You can also hash text directly without creating a file. Pass the string with echo and pipe it into md5sum. Use the -n flag on echo to suppress the trailing newline, because that invisible character would otherwise change the hash:

echo -n "LinuxForDevices" | md5sum
68cd249253ae59f05b60af56bdcab1bc  -

The dash at the end means the input came from standard input rather than a named file. If your hash differs from a published value for supposedly identical text, a missing -n is the usual suspect.

Useful md5sum Options

OptionEffect
-bRead the file in binary mode
-tRead the file in text mode (the default)
-cRead checksums from a file and verify them
–tagOutput in BSD-style format
–strictExit non-zero if the checksum file is malformed
-wWarn about improperly formatted checksum lines

The full option list lives in the manual page, which you can open as described in the man command guide.

Encode and Decode with the base64 Command

The base64 command reads input, encodes or decodes it, and writes the result to standard output or a file. The basic syntax is:

base64 [option] [input_file]

Encoding our example file turns every byte into printable ASCII characters:

base64 example.txt
SGkgdGhpcyBpcyBhIHRlc3QgZmlsZSBmcm9tIExpbnV4Rm9yRGV2aWNlcwo=

Decoding reverses it exactly. The -d flag tells the command to treat its input as Base64 and reconstruct the original bytes:

base64 -d encoded.txt
echo -n "TGludXhGb3JEZXZpY2Vz" | base64 -d

Both forms print the original text unchanged. Because encoding is fully reversible, Base64 provides zero confidentiality.

Never treat an encoded password or API key as hidden. Anyone can decode it with a single command.

Useful base64 Options

OptionEffect
-eEncode the input (the default)
-dDecode Base64 data back to original bytes
-w 0Disable line wrapping for one long output line
-iIgnore non-alphabet characters when decoding
–helpShow usage summary

The -w 0 option matters more than it looks.

By default the encoder wraps its output every 76 characters, which breaks embedded strings in JSON fields, URLs, and config files. Decode such strings only with wrapping tolerated.

Verify a Downloaded File Against Its Published Checksum

This is the workflow you will actually repeat. Projects publish a checksum file next to their downloads, and verification means comparing your copy against that published value.

  1. Save the official checksum line to a file, or generate one locally for this demonstration:
md5sum example.txt > example.md5
cat example.md5
9614a183699d93ca7d0409699f384767  example.txt
  1. Verify while the file matches its recorded hash. The -c flag reads the checksum file and recomputes each digest:
md5sum -c example.md5
example.txt: OK
  1. Change the file by even one appended line and re-verify. The check now fails loudly instead of passing silently:
printf 'new line\n' >> example.txt
md5sum -c example.md5; echo "exit code: $?"
example.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
exit code: 1

The non-zero exit code matters for scripts. Wrap verification in an if statement or chain it with an AND-list so a corrupted download stops your pipeline before the bad file gets used.

To fetch a checksum over the network alongside your download, pull it with curl or wget first. The verification step here completes that workflow either way.

Why Decoded Base64 Ignores Tampering

The failed md5sum check above exposes the core difference between the two tools. When the file changed, md5sum noticed immediately because it compares content fingerprints.

A saved Base64 copy behaves differently. Decoding it returns the original bytes regardless of what later happened to example.txt, because the decoder processes whatever string you hand it and has no memory of the source file.

In short: Base64 answers “what are these bytes”, md5sum answers “are these bytes the ones I expected”. Choosing between them is choosing between those two questions.

Frequently Asked Questions

Can I decrypt an MD5 hash to get the original text?

No. MD5 is a one-way hash function, so there is no decryption. Tools that claim to crack MD5 simply guess many inputs until one produces the matching hash, which works mainly for weak passwords. For file integrity checking, no reversal is needed because you compare hashes rather than recover content.

Is Base64 encryption?

No. Base64 is an encoding scheme, not encryption. It converts binary data into printable ASCII characters and anyone can reverse it with base64 -d. It offers no confidentiality whatsoever.

Is MD5 still safe to use?

Not for security purposes. Collision attacks against MD5 are well established, so use SHA-256 via sha256sum for signatures, passwords, or anything an attacker could influence. MD5 remains acceptable for detecting accidental corruption in downloads when the publisher only provides an MD5 checksum.

How do I encode a string directly with base64 without a file?

Use echo with the -n flag and a pipe: echo -n “your text” | base64. The -n suppresses the trailing newline so the output encodes exactly your text. Decode with echo -n “encoded-string” | base64 -d.

Why does my md5sum differ from the published checksum?

The most common causes are a truncated or corrupted download, a different file version than the checksum was made for, or comparing against a SHA-256 value with md5sum. Confirm which algorithm the publisher used, then re-download and verify again.

Start with md5sum whenever you download system images, archives, or packages, and reach for base64 when a text-only interface demands them. Both ship with GNU coreutils on every mainstream distribution, so nothing needs installing. For broader context on shell work, see the Linux command-line guide and the walkthrough on editing files in Linux.