Base64 encoding length parameter

I decode a base64 string by modifying it and transcoding it using Ruby. The problem with re-encoding is that the ruby-encoding library adds a line after 60 characters. How can I say that there are no maximum characters per limit limit?

val = "==" decoded_val = Base64.decode64(val) encoded_val = Base64.encode64(val) #=> QmFzZTY0IGlzIGEgZ2VuZXJpYyB0ZXJtIGZvciBhIG51bWJlciBvZiBzaW1p # bGFyIGVuY29kaW5nIHNjaGVtZXMgdGhhdCBlbmNvZGUgYmluYXJ5IGRhdGEg # YnkgdHJlYXRpbmcgaXQgbnVtZXJpY2FsbHkgYW5kIHRyYW5zbGF0aW5nIGl0 # IGludG8gYSBiYXNlIDY0IHJlcHJlc2VudGF0aW9uLiBUaGUgQmFzZTY0IHRl # cm0gb3JpZ2luYXRlcyBmcm9tIGEgc3BlY2lmaWMgTUlNRSBjb250ZW50IHRy # YW5zZmVyIGVuY29kaW5nLg0KDQpCYXNlNjQgZW5jb2Rpbmcgc2NoZW1lcyBh # cmUgY29tbW9ubHkgdXNlZCB3aGVuIHRoZXJlIGlzIGEgbmVlZCB0byBlbmNv # ZGUgYmluYXJ5IGRhdGEgdGhhdCBuZWVkcyBiZSBzdG9yZWQgYW5kIHRyYW5z # ZmVycmVkIG92ZXIgbWVkaWEgdGhhdCBhcmUgZGVzaWduZWQgdG8gZGVhbCB3 # aXRoIHRleHR1YWwgZGF0YS4gVGhpcyBpcyB0byBlbnN1cmUgdGhhdCB0aGUg # ZGF0YSByZW1haW5zIGludGFjdCB3aXRob3V0IG1vZGlmaWNhdGlvbiBkdXJp # bmcgdHJhbnNwb3J0LiBCYXNlNjQgaXMgdXNlZCBjb21tb25seSBpbiBhIG51 # bWJlciBvZiBhcHBsaWNhdGlvbnMgaW5jbHVkaW5nIGVtYWlsIHZpYSBNSU1F # LCBhbmQgc3RvcmluZyBjb21wbGV4IGRhdGEgaW4gWE1MLg== 
+4
source share
1 answer

RFC 4648: Base16, Base32, and Base64 encodings allow you to:

3.3. Interpreting non-alphabet characters in encoded data
[...]
Implementations MUST reject encoded data if they contain characters outside the base alphabet when interpreting the base data encoding, unless the specification referencing this document explicitly states otherwise. Such specifications may instead indicate how MIME does that characters outside the base coding alphabet should simply be ignored when interpreting the data ("be liberal in what you accept"). Note that this means that any adjacent carriage returns / strings (CRLF) constitute "non-alphabet characters" and are ignored.

So, newlines are fine, and almost everyone ignores them, even if they don’t comply with RFC 4648.

In addition, the excellent guide has the following:

encode64 (bin)

Returns a Base64 encoded bin version. This method complies with RFC 2045. Line channels are added to each 60 encoded [sic] characters.

Thus, a string length of 60 characters is intentional and predetermined. If you need a strict RFC 4648 Base64 (i.e. no new lines) then strict_encode64 :

strict_encode64 (bin)

Returns a Base64 encoded bin version. This method complies with RFC 4648. No line channels are added.

So you can say Base64.strict_encode64(val) to get the result you are looking for.

And for reference, here is the relevant section of RFC 2045 :

6.8. Base64 Content-Transfer-Encoding
[...]
The encoded output stream must be represented in lines of no more than 76 characters. All line breaks or other characters not found in Table 1 should be ignored using decoding software.

Thus, a string length of 60 characters is somewhat arbitrary, but corresponds to RFC 2045 with 60 < 76 .

+8
source

Source: https://habr.com/ru/post/1444978/


All Articles