![]() Thanks to eel ghEEz for pointing this out in the comments.Īs said in other responses, there are various ways in which base64 data could be corrupted. However, if validate is False (or left blank to be the default) you can blindly add two padding characters without any problem. If validate is True, these non-alphabet characters in the input result in a binascii.Error. ![]() If validate is False (the default), characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is True this will result in a binascii.Error being raised if the total padding is longer than two characters. b"aGVsbG8="), this approach will only work if the validate keyword argument is set to False (which is the default). Note that if the string s already has some padding (e.g. ![]() Which is simpler than: base64.b64decode(s + b'=' * (-len(s) % 4)) This lets you write: base64.b64decode(s + b'=') ![]() What this means is that you can just add the maximum number of padding characters that you would ever need-which is two ( b'=')-and base64 will truncate any unnecessary ones. So, something like: b'abc=' works just as well as b'abc=' (as does b'abc='). There are many other answers on this question, but I want to point out that (at least in Python 3.x) base64.b64decode will truncate any extra padding, provided there is enough in the first place. It seems you just need to add padding to your bytes before decoding. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |