Module: Base64URL

Defined in:
lib/base64url.rb

Overview

The Base64URL module provides for the encoding and decoding of binary data using a base64url representation.

base64url encoding is Base64 for URLs and filenames. base64url’s 62nd and 63rd chars are ‘-’ and ‘_’. base64url does not have pad chars ‘=’.

Example

A simple encoding and decoding.

require "base64url"

enc   = Base64URL.encode('Send reinforcements')
                    # -> "U2VuZCByZWluZm9yY2VtZW50cw"
plain = Base64URL.decode(enc)
                    # -> "Send reinforcements"

Class Method Summary collapse

Class Method Details

.decode(bin) ⇒ Object

Returns the base64url-decoded version of bin.



33
34
35
36
37
# File 'lib/base64url.rb', line 33

def decode(bin)
  m = bin.size % 4
  bin += '=' * (4 - m) if m != 0
  bin.tr('-_', '+/').unpack('m0').first
end

.encode(bin) ⇒ Object

Returns the base64url-encoded version of bin.



28
29
30
# File 'lib/base64url.rb', line 28

def encode(bin)
  [bin].pack('m0').gsub(/\=+\Z/, '').tr('+/', '-_')
end