Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It is worth noting there's a difference in the base64.b64encode function, vs the default MIME Base64 codec on strings[2].

Per RFC2045[1]:

    (Soft Line Breaks) The Quoted-Printable encoding
    REQUIRES that encoded lines be no more than 76
    characters long.  If longer lines are to be encoded
    with the Quoted-Printable encoding, "soft" line breaks
Due to the insertion of these soft-line breaks, encoding is not the same, as you can verify yourself:

    import os
    import base64
    import unittest


    class Base64Test(unittest.TestCase):

        def test_long_string_base64_decoding_and_encoding(self):
            byte_seq = os.urandom(500)
            mime64_encoded = byte_seq.encode('base64')
            self.assertNotEqual(base64.b64encode(byte_seq), mime64_encoded)
            self.assertEqual(base64.b64decode(mime64_encoded),
                             mime64_encoded.decode('base64'))


    if __name__ == '__main__':
        unittest.main()
Decoding, as you can see above, is fine. This makes a difference when encoding a really long string in an HTTP header.

[1] http://www.ietf.org/rfc/rfc2045.txt [2] http://docs.python.org/library/codecs.html#standard-encoding...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: