Class: FuzzyNotes::Cipher

Inherits:
Object
  • Object
show all
Includes:
Authentication, Logger
Defined in:
lib/fuzzy_notes/cipher.rb

Constant Summary collapse

PLAINTEXT_EXT =
'txt'
CIPHERTEXT_EXT =
'enc'
TMP_FILE_PREFIX =
'fuzzy_notes'
ENCRYPT_METHOD_MATCHER =
/\Aencrypt_(data|file|files|from_tempfile|from_tempfiles)\Z/
DECRYPT_METHOD_MATCHER =
/\Adecrypt_(data|file|files|to_tempfile|to_tempfiles)\Z/

Constants included from Logger::Colors

Logger::Colors::CREATE_COLOR, Logger::Colors::DEFAULT_COLOR, Logger::Colors::DELETE_COLOR, Logger::Colors::EXPORT_COLOR, Logger::Colors::IMPORT_COLOR, Logger::Colors::NOTE_COLOR, Logger::Colors::NUMBER_COLOR, Logger::Colors::PATH_COLOR, Logger::Colors::USER_COLOR

Method Summary

Methods included from Authentication

#get_password, #get_username

Methods included from Logger

#log

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args) ⇒ Object (private)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fuzzy_notes/cipher.rb', line 15

def method_missing(method_sym, *args)
  method_name = method_sym.to_s
  log.debug "args: #{args.inspect}"
  last_arg = args.last
  opts = last_arg.is_a?(Hash) ? last_arg : {}

  case method_name
  when ENCRYPT_METHOD_MATCHER
    @action = :enc
  when DECRYPT_METHOD_MATCHER
    @action = :dec
  else super
  end

  case $1
  when 'data'
    process_data(args.first, Gibberish::AES.new(get_password))
  when 'file'
    process_file(args.first, Gibberish::AES.new(get_password), opts)
  when 'files'
    cipher = Gibberish::AES.new(get_password)
    args.first.map { |path| process_file(path, cipher, opts) }
  when 'to_tempfile', 'from_tempfile'
    self.send(method_sym, args.first, Gibberish::AES.new(get_password))
  when 'to_tempfiles', 'from_tempfiles'
    cipher = Gibberish::AES.new(get_password)
    args.first.map { |file_path| self.send(method_name[0..-2], *file_path, cipher) }
  end
end