What is base64 encoding/decoding and how we can benefit from this representation
Base64 schemes represent binary data in an ASCII string format (English alphabet, common punctuation chars, control chars) by translating it into a base-64 representation. This basically means that all kind of characters (ASCII, UTF8, UTF16…) with control characters can be mapped for example in English alphabet a-z, A-Z, 0-9 and you would be able to read them all on screen, or even print them out.
First of all, Magento is using base64 encode/decode each time you want to add product in your cart. Magento will redirect you to something like this: “http://loc.magento.com/checkout/cart/add/uenc/aHR0cDovL2xvYy5tYWdlbn…93/” with some POST data (product=93&related_product=&super_attribute=37&qty=2).
These chars “aHR0cDovL2xvYy5tYWdlbnRvLmNvbS9hc2ljcy1tZW4tcy1nZWwta2F5YW5vLXhpaS5odG1s” are base64 encoded strings and POST data you can see (HTTP header traffic) with Firebug, LiveHTTPHeaders (addon for FF), Charles proxy,…
If you don’t know what does it mean (encoded chars) and how they are generated – keep reading.
Additionally, if you have any useful example where did you use base64 encode/decode, feel free to share your ideas with others.
From wiki: “Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport”.
In our case uenc = url encoded = aHR0cDovL2xvYy5tYWdlbnRvLmNvbS9hc2ljcy1tZW4tcy1nZWwta2F5YW5vLXhpaS5odG1s = http://loc.magento.com/asics-men-s-gel-kayano-xii.html.
What do you think why doesn’t Magento use normal strings in URL instead of base64 which is around 33% longer? Hint: you’ll get this later in the article…
As you can see we can’t put char “/” in URL if we want to send last visited URL as parameter because we would break attribute/value rule for “MOD rewrite” – GET parameter.
A full example would be: “http://loc.magento.com/checkout/cart/add/uenc/http://loc.magento.com/asics-men-s-gel-kayano-xii.html/product/93/” – sure it’s wrong.
This is just one example where you can use base64. Also in administration part when you search in grid in some column some value you can see that Magento is using base64 to encode your filter(s). In other words, you don’t need to worry about which character sets client’s browser can accept. You are sure that all characters will be transferred over the wire with base64!
Another example could be the following: you have a web service that accept only ASCII chars. You want to save and then transfer user’s data to some other location (API) but recipient want receive untouched data. Again base64 is for that also. The only downside is that base64 encoding will require around 33% more space than regular strings.
So with base64 you can encode and transfer any sets of binary data through any system and then decode them to original binary data. Cool, right?
Does gmail use base64? Sure!
Instead of “reinventing the wheel” and showing you how base64 works please take a look this pdf where you can see nice examples and table with ASCII chars. http://www.aardwulf.com/tutor/base64/base64.pdf
You can also build your own base64 algorithm for your specific needs!
For the end, did you maybe buy some extension that has weird characters in it and you didn’t know how to see source code? If your answer is yes, while reading this article did you maybe recognize that the code you have is probably base64 encoded? And can you perhaps see “eval()” function in that code?
If the answer is yes, then in my next article I’ll show you how to do “reverse engineering” and see your source code for bunch of extensions,… which you can buy and which are base64 encoded.