Home / Blog / Developer & Web

Developer & Web

Base64 Hash Output: Encode Digest Bytes or HEX Text?

Represent a SHA-256 digest as Base64, distinguish raw digest bytes from the printed hexadecimal string, and verify the expected output length and byte round trip.

Base64 Hash Output: Encode Digest Bytes or HEX Text?

Separate the hash operation from its display format

Base64 is an encoding of bytes, not a hash algorithm. A request for a Base64 hash normally means compute a digest using a named algorithm, then display the digest bytes as Base64. SHA-256, for example, produces 32 bytes regardless of whether those bytes are later printed as hexadecimal or Base64. Encoding the digest differently does not change the algorithm or add encryption. Specify both parts of the operation, such as SHA-256 with standard Base64 output, so another system can interpret the result without guessing what was hashed or how it was represented.

Follow the abc example through the digest

For the three UTF-8 bytes representing abc, the SHA-256 digest in hexadecimal is ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad. The same 32 digest bytes encoded as standard Base64 are ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=. These are two representations of the same digest. They do not resemble one another visually because hexadecimal groups bits in fours while Base64 groups them in sixes. Decode each representation into bytes before comparing them; a direct string comparison across different encodings is not meaningful.

Do not encode the printed HEX characters by mistake

The hexadecimal digest above contains 64 printable ASCII characters. Passing that text to an ordinary text-to-Base64 tool encodes those 64 character bytes rather than the 32 original digest bytes. The resulting Base64 begins YmE3ODE2 and is 88 characters long with padding. It is a valid encoding of a different byte sequence, but it is not the standard Base64 representation of the digest. This is a frequent mismatch when one service returns HEX and another expects raw digest bytes encoded as Base64. Identify the input type to the encoding step before choosing a converter.

Check the expected lengths

SHA-256 has 256 bits, which is 32 bytes. Two hexadecimal characters per byte give 64 HEX characters. Standard Base64 uses groups that map three bytes to four characters, so 32 bytes require 44 characters including one final equals sign for padding. By contrast, encoding the 64-byte HEX text requires 88 Base64 characters including two padding signs. These length checks can reveal a double-encoding mistake quickly, though a plausible length alone cannot prove that the content is correct. Other hash algorithms have different digest lengths, so do not apply the SHA-256 length rule to every hash.

Ask a byte-aware API for the desired representation

In Node.js, createHash('sha256').update('abc', 'utf8').digest('base64') directly returns the standard Base64 digest. If a trusted, validated HEX digest is already available, Buffer.from(hex, 'hex').toString('base64') changes its representation without hashing it again. In the latter operation, the original message is not required because the conversion acts only on digest bytes. Rehashing the HEX string would create a new digest of the displayed text. Validate a supplied HEX value's characters and expected length before treating it as a complete digest.

Respect the local text tools' boundaries

The local SHA-256 Generator reports hexadecimal output for the text entered. Its companion Base64 text tools encode text; pasting that hexadecimal result into them therefore follows the 64-character text path described above. The site does not provide a dedicated raw-digest-to-Base64 switch in this calculator. Use the documented byte-buffer operation when a protocol requires the Base64 digest. Keeping this limitation visible is better than labelling the output of a text encoder as equivalent to an API header or signature field that expects a different byte representation.

Verify the exact output contract

Before sending a digest to another program, check whether it expects standard Base64 or Base64url, whether padding is required and which source bytes are hashed. A protocol may define a canonical message representation, so whitespace or line-ending changes can matter before hashing even though the final output encoding is correct. For the worked example, decode the 44-character Base64 value and verify that it contains 32 bytes whose hexadecimal form matches the printed SHA-256 digest. Preserve the algorithm and encoding labels with a saved value. Base64 changes readability and transport syntax; it does not authenticate the message or make a digest reversible.

Use the SHA-256 Generator

Open SHA-256 Generator

Source and scope

Node.js: choosing the encoding of a hash digest

Node.js: byte buffers and character encodings