Settings

Theme

Ruby Padding Issues with Base64 Decoding

blog.vidyard.com

9 points by michaelrlitt 13 years ago · 2 comments

Reader

cmwright 13 years ago

Great article!

Why is it that the encoded string needs to be divisible by four before you can decode it?

  • mpyne 13 years ago

    If I remember the decoding process right, it's basically just taking 4 characters at a time, multiplying by (6/8) (which is 3/4) to get 3 characters.

    This doesn't make it impossible to decode less than 4 characters, but you can't tell if the shortage is due to an error (early truncation) or just the padding being stripped (the article doesn't make it clear, but = is the traditional padding character used to make the encoded text have the proper length). Obviously both encoder/decoder will need to agree on the padding style to be used if it is important that encoded text be canonical.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection