ColdFusion's binaryDecode(input, 'base64')
is picky because padding is mandatory.
What is the correct way to add padding with =
to a base64 value?
1.) Ben Nadel uses:
value &= repeatString( "=", ( 4 - ( len( value ) % 4 ) ) );
<cfset res = Len(raw_str) % 4>
<cfif res eq 2>
<cfset raw_str &= "==">
<cfelseif res eq 3>
<cfset raw_str &= "=">
</cfif>
While they both seem to work, the 1st solution may return 1 to 4 =
's, while the 2nd solution may return 0, 1 or 2 =
's. Wikipedia on Base64 Padding seems to indicate that a valid base64 value should really have only 1 or 2 =
's.
1st solution seems to work with all base64 value lengths, but it may sometimes return 3 or 4 =
's which is kind of strange. Where as the 2nd solution may fail for base64 value that has remainder of 1. CF throws The input and output encodings are not same.