Class: Ciphr::Functions::Base::Base64

Inherits:
Base show all
Defined in:
lib/ciphr/functions/base_radix.rb

Instance Attribute Summary

Attributes inherited from InvertibleFunction

#invert

Attributes inherited from Function

#args, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InvertibleFunction

invertable?

Methods inherited from Function

inherited, #initialize, invertable?, #prepend, #read

Constructor Details

This class inherits a constructor from Ciphr::Functions::Function

Class Method Details

.alignedObject



120
121
122
# File 'lib/ciphr/functions/base_radix.rb', line 120

def self.aligned
  nil # preserves alignment
end

.paramsObject



150
151
152
# File 'lib/ciphr/functions/base_radix.rb', line 150

def self.params 
  [:input]
end

.variantsObject



140
141
142
143
144
145
146
147
148
# File 'lib/ciphr/functions/base_radix.rb', line 140

def self.variants
  chars = {"+"=>"p", "-"=>"h", "_"=>"u", ":"=>"c", "/"=>"s", "." => "d", "!"=>"x", "="=>"q"}
  types = {"+/=" => ["std"], "+/" => "utf7", "+-" => "file", "-_" => "url", "._-" => "yui", 
           ".-" => "xml-name", "_:" => "xml-id", "_-" => "prog-id-1", "._" => "prog-id-2", "!-" => "regex"}
  variants = types.map{|c,n| [["b64","base64"].product([c.chars.map{|c| chars[c] }.join,n]).map{|a| a.join("-")}, {:chars => c}]}
  std = variants.select{|v| v[0].include? "b64-std"}[0] #add short aliases for standard
  std[0] = ["b64","base64"].concat(std[0])
  variants
end

Instance Method Details

#applyObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ciphr/functions/base_radix.rb', line 124

def apply    
  input = @args[0]
  if !invert
    Proc.new do
      chunk = input.read(3)
      chunk && ::Base64.encode64(chunk).gsub(/\s/,'').tr("+/", options[:chars][0,2]).tr("=", options[:chars][2,3])
    end
  else
    Proc.new do
      chunk = input.read(4)
      chunk = chunk && chunk + "="*(4-chunk.size) #pad
      chunk && ::Base64.decode64(chunk.tr(options[:chars][0,2],"+/").tr(options[:chars][2,3],"=").ljust(4,"="))
    end
  end
end