Module: SecurizeString::Base64Methods::InstanceMethods

Defined in:
lib/securize_string/base64_methods.rb

Overview

Adds instance methods for Base64 support via inclusion of SecurizeString::Base64Methods to a class.

Instance Method Summary collapse

Instance Method Details

#from_base64(opts = {}) ⇒ Object

Decodes from base64.

By default, this decodes the normal RFC 2045 Base64.

If the url_safe option is set to true, then it decodes the RFC 4648 encoding, which uses a slightly different encoding mechanism which is sometimes compatible, but often incompatible with RFC 2045.

Raises:

  • (ArgumentError)


40
41
42
43
44
# File 'lib/securize_string/base64_methods.rb', line 40

def from_base64(opts={})
  raise ArgumentError, "__method__ expects an argument hash but got #{opts.class.name}" unless opts.kind_of?(Hash)
  string = (opts[:url_safe] ? urlsafe_decode64(self) : Base64.decode64(self))
  return self.class.new(string)
end

#to_base64(opts = {}) ⇒ Object

Encodes to Base64.

By deault, this is the normal RFC 2045 Base64 encoding.

If the url_safe option is set to true, then the RFC 4648 encoding is used, which uses a slightly different encoding mechanism which is sometimes compatible, but often incompatible with RFC 2045.

If the nobreak option is set to true, all line feeds are removed from the input.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
# File 'lib/securize_string/base64_methods.rb', line 26

def to_base64(opts={})
  raise ArgumentError, "__method__ expects an argument hash but got #{opts.class.name}" unless opts.kind_of?(Hash)
  data = (opts[:url_safe] ? urlsafe_encode64(self) : Base64.encode64(self))
  data.delete!("\n\r") if opts[:no_break] # Delete on \n and \r is about 3x faster than gsub on /\s+/.
  return self.class.new(data)
end