Search code examples
hashencodingbase64url

Determine the length of a base64url encoded string


I am attempting to write a unit test to check that I'm hashing and encoding a string (actually, PEM certificate) correctly.

What I'm doing is:

  1. Taking a certificate (in PEM format)
  2. Convert it to DER
  3. Run a hashing function on it to get a hashed value (SHA-1 / SHA-256)
  4. Base64Url encoding the hash

The value of a SHA-256 hash has a constant length (256 bits). This can be represented differently (hex / binary, etc) but it's the same underneath. I'm storing the value in a Java byte array.

My question is this: if I attempt to base64url encode this hashed byte array value, is the length of the resulting encoded string always the same? Or will it vary depending on values present in the underlying hash?

What I'm not clear on is the "url" part of the "base64url" encoding: because it makes the string safe for urls, does it do any sort of character expansion (for example, urlencoding replaces a single space character with %20 which is three characters).


Solution

  • Base64Url encoding is simply Base64 encoding with the + and / switched to two different characters that are better for putting into a Uri.

    Since SHA-256 will always be the same fixed length, a base64-encoded SHA-256 hash will also be that same fixed length. Each base64 digit is 6 bits. While you will get some padding characters ('=') due to 256/6 not being an integer, the string will always be the same length.