Module: Nuntius::Encodings::URLSafeBase64

Defined in:
lib/nuntius/encodings/url_safe_base64.rb

Overview

Encode/Decode messages using RFC4648 Base 64 Encoding with URL and Filename Safe Alphabet http://tools.ietf.org/html/rfc4648#section-5

Constant Summary collapse

BASE_CHARACTERS =
"+/"
REPLACEMENT_CHARACTERS =
"-_"
PADDING_CHARACTER =
"="

Class Method Summary collapse

Class Method Details

.decode(bin) ⇒ Object



15
16
17
18
19
20
# File 'lib/nuntius/encodings/url_safe_base64.rb', line 15

def self.decode(bin)
  padding = (4 - (bin.length % 4)) % 4
  ( bin.tr(REPLACEMENT_CHARACTERS, BASE_CHARACTERS) + ( PADDING_CHARACTER * padding ) ).unpack("m0").first
rescue ArgumentError
  raise Nuntius::Encodings::DecodingError.new 'Invalid Base64'
end

.encode(bin) ⇒ Object



11
12
13
# File 'lib/nuntius/encodings/url_safe_base64.rb', line 11

def self.encode(bin)
  [bin].pack("m0").tr(BASE_CHARACTERS,REPLACEMENT_CHARACTERS).gsub(PADDING_CHARACTER,'')
end