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[502]=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!
So, what do I need to do to convert characters into base64 encoded schema? Just a little bit of math or you can use any trusted code which will do that for you. You have bunch of “online base64 decode/encode” scripts on web, just google them. What programming language to use to develop scripts for such of things? Sure, javascript to send “safe” data over the wire and you can use PHP to decode them, vice-versa.
Additionally, you can find base64 encoding algorithms in javascript in Magento’s folder webroot/js/mage/adminhtml/hash.js around line 57, function encode_base64( what ) {…
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!
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.
Stay tuned!