Sha256: f5cec50537f83ccaa519135f8d73ec6c132900406e1731f4670a370a94621100

Contents?: true

Size: 750 Bytes

Versions: 3

Compression:

Stored size: 750 Bytes

Contents

module Nuntius
  module Encodings

    # Encode/Decode messages using RFC4648 Base 64 Encoding
    # with URL and Filename Safe Alphabet {http://tools.ietf.org/html/rfc4648#section-5}
    module URLSafeBase64
      BASE_CHARACTERS = "+/"
      REPLACEMENT_CHARACTERS = "-_"
      PADDING_CHARACTER = "="

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

      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

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nuntius-0.1.2 lib/nuntius/encodings/url_safe_base64.rb
nuntius-0.1.1 lib/nuntius/encodings/url_safe_base64.rb
nuntius-0.1.0 lib/nuntius/encodings/url_safe_base64.rb