Encode hash values as Base64

When to Base64-encode a hash digest instead of using hex: what changes, how much shorter it gets, and where case, symbols and padding cause trouble.

A cryptographic hash produces a fixed number of raw bytes. Those bytes are not printable text, so before a digest can be shown, stored in a text field or passed in a header, it has to be encoded. Hexadecimal is the usual default. Base64 is the common alternative. This review covers whether Base64 is the right choice for a given use, what the encoding actually does, and where the result becomes awkward to read, copy or share.

Is Base64 suitable for your hash values?

Base64 is a good fit when the receiving system expects it or when the length of the digest matters. It represents the same bytes in fewer characters than hex. A SHA-256 digest is 64 characters in hex and 44 characters in standard Base64, including padding. Several documented web conventions use Base64 digests. Subresource Integrity attributes are one example. Digest-style HTTP headers are another. In these cases the specification decides the format for you, and hex would simply be wrong.

The decisive tradeoff is human handling. Base64 output is case-sensitive and can contain plus signs, slashes and equals signs. That makes it harder to read aloud, compare by eye, type by hand, or use safely in URLs and file names. Hex uses only 0–9 and a–f. It is easier to inspect, and an upper-case and a lower-case hex string still describe the same bytes.

Hex is usually the better choice in the following situations:

  • People will compare digests visually, read them over the phone or type them by hand.
  • The value will be used as a file name, especially on a file system that ignores letter case.
  • The surrounding ecosystem already uses hex. Checksum files, sha256sum-style command output and Git object IDs all do.
  • The value may pass through tools or searches that are case-insensitive or treat punctuation as a separator.
  • There is no space constraint and no specification that requires Base64.

In short, use Base64 when a format requires it or compactness is worth the loss of readability. Use hex when people are the main audience. If a digest is placed in a URL, path or file name, use the URL-safe Base64 variant described below.

What the encoding actually changes

Base64 changes the stored characters, not just how they look. It is not a font, a mirrored style or a display effect. It takes the digest bytes three at a time, splits each 24-bit group into four 6-bit values, and maps each value to one of 64 ASCII characters: A–Z, a–z, 0–9, plus sign and slash. When the byte count is not a multiple of three, one or two equals signs pad the final group. The output is new text that must be decoded to recover the original bytes.

Encoding does not change the hash itself. The digest bytes are identical whether they are written in hex or Base64. Converting between the two is lossless. Base64 does not hide, encrypt or shorten the underlying information. Anyone can decode it, and the digest offers the same integrity guarantees in either form.

The most common mistake is encoding the wrong input. Many tools print a digest as a hex string. If you Base64-encode that 64-character hex text, you are encoding the characters of the hex string, not the 32 digest bytes. The result is 88 characters long and will not match what a system expecting a Base64 digest computes. Always encode the raw digest bytes, or decode the hex back to bytes first.

The commands below show both correct approaches for the SHA-256 digest of empty input. The output values are the well-known digests for an empty string.

# Hex digest of empty input
printf '' | sha256sum
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  -

# Base64 of the raw digest bytes
printf '' | openssl dgst -sha256 -binary | base64
47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=

# Converting an existing hex digest to Base64
printf 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' | xxd -r -p | base64
47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=

The lengths follow from the mechanism. Thirty-two bytes form ten full 3-byte groups plus two leftover bytes. That gives 44 characters with one padding sign. A 16-byte MD5 digest leaves one leftover byte, so its Base64 form is 24 characters ending in two padding signs. The MD5 digest of empty input, d41d8cd98f00b204e9800998ecf8427e in hex, becomes 1B2M2Y8AsgTpgAmY7PhCfg== in Base64.

Character reversal concerns such as combining marks or multi-code-point emoji do not apply to the output. Base64 produces plain ASCII only. Unicode handling matters earlier, when the original text is turned into bytes before hashing. The same visible text can produce different digests if it is encoded or normalized differently. Base64 has no part in that problem.

Standard and URL-safe variants

The URL- and filename-safe alphabet replaces the plus sign with a hyphen and the slash with an underscore. Padding is often omitted. In that form the SHA-256 example above becomes 47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU. The two variants decode to the same bytes, but a decoder expecting one may reject the other or misread it. Specifications that use Base64 usually state which alphabet and padding rule they require. Check that before choosing.

Property | Hex | Standard Base64 | URL-safe Base64
SHA-256 length | 64 characters | 44 characters with padding | 43 characters without padding
Characters used | 0–9, a–f | A–Z, a–z, 0–9, plus, slash, equals | A–Z, a–z, 0–9, hyphen, underscore
Case-sensitive | No | Yes | Yes
Safe in URLs and file names without escaping | Yes | No | Generally yes
Ease of visual comparison | Higher | Lower | Lower

Will the result remain readable and usable after copying or sharing?

Because the output is plain ASCII, it usually survives copy and paste intact. Most practical problems come from how destinations treat the special characters and letter case:

  • URLs and query strings: in standard Base64, a plus sign in a query string may be decoded as a space, and a slash can be read as a path separator. Use the URL-safe variant or percent-encode the value.
  • File names: a slash cannot appear in a file name on common systems. On case-insensitive file systems, two Base64 values that differ only in letter case can collide.
  • Search and comparison: case-insensitive search or comparison can treat different Base64 digests as equal. Comparisons must be exact.
  • Line wrapping: some Base64 encoders insert line breaks at a fixed width, as MIME-style output does. A digest copied with an embedded newline or trailing whitespace will fail comparison unless it is stripped.
  • Selection and editing: punctuation inside the string may stop double-click selection partway in some editors and terminals, so part of the value can be copied by accident. This depends on the application and has not been verified for any particular one here.

For accessibility, a long string of mixed-case letters, digits and symbols is hard to follow by ear. How a given screen reader announces case changes and punctuation depends on its settings. No documentation or test results for specific assistive technologies were supplied for this review, so no claims about particular screen readers are made here. If people need to verify a digest by listening to it or reading it aloud, hex, or a shortened hex fingerprint alongside the full value, is the more practical choice.

Provide a plain hex equivalent when a digest is shown to people for manual checking, published next to a download for users to verify, or likely to pass through systems whose handling of case and punctuation is unknown. Machine-to-machine fields defined as Base64 do not need one.

Gaps and cautions

Behavior in specific apps, browsers, messaging services and assistive technologies has not been tested for this review. The usability points above are based on how the encoding works and on documented URL and file-name rules, not on scoped tests of named destinations. If you rely on a web-based converter, its appearance or description says nothing about how it handles or retains what you paste. Hashes of passwords or other secrets can be sensitive, so computing and encoding them locally with standard tools avoids that uncertainty.