module

Base64

v1_9_3_392 - Show latest stable

The Base64 module provides for the encoding (#encode64, #strict_encode64, #urlsafe_encode64) and decoding (#decode64, #strict_decode64, #urlsafe_decode64) of binary data using a Base64 representation.

Example

A simple encoding and decoding.

require "base64"

enc   = Base64.encode64('Send reinforcements')
                    # -> "U2VuZCByZWluZm9yY2VtZW50cw==\n"
plain = Base64.decode64(enc)
                    # -> "Send reinforcements"

The purpose of using base64 to encode data is that it translates any binary data into purely printable characters.

Files

  • lib/base64.rb

1Note

Real life use

Soleone ยท Feb 12, 20094 thanks

If you're wondering what the base64 format is used for, here are some examples:

  • HTTP Basic authentication: encode your username and password as one string, and add it as a header of an HTTP request. When a page requiring basic authentication gets called from a browser it results in a generic Username/Password dialog from that browser. See also http://en.wikipedia.org/wiki/Basic_access_authentication
  • Encode the binary content of images to base64 and embed it in XML documents, for example in web services
  • For more information see http://en.wikipedia.org/wiki/Base64

Just note that the encoded (character) data is about 30% larger than un-encoded (binary) data.