Module: RightSupport::Data::Base64URL

Defined in:
lib/right_support/data/base64_url.rb

Overview

Implements the base64url encoding mechanism as described in RFC7515: JSON Web Signature (JWS).

This encoding is similar to base64, but omits newlines and padding ‘=` characters, and uses the `-` and `_` characters in place of the `+` and `/` characters. This makes it suitable for use inside URLs, cookies, and other “webby” contexts where we want to avoid text expansion due to URL encoding.

Class Method Summary collapse

Class Method Details

.decode(b64) ⇒ String

Returns plain ASCII.

Parameters:

  • str (String)

    base64 to decode to ASCII

Returns:



21
22
23
24
25
# File 'lib/right_support/data/base64_url.rb', line 21

def self.decode(b64)
  b64 = b64.dup
  b64 += '=' * (4 - b64.length.modulo(4))
  Base64.decode64(b64.tr('-_', '+/'))
end

.encode(text) ⇒ String

Returns base64 representation of str.

Parameters:

  • str (String)

    ASCII string to encode

Returns:

  • (String)

    base64 representation of str



15
16
17
# File 'lib/right_support/data/base64_url.rb', line 15

def self.encode(text)
  Base64.encode64(text).tr('+/', '-_').gsub(/[\n=]/, '')
end