Class: Reedb::RAES

Inherits:
MCypher show all
Defined in:
lib/reedb/security/aes.rb

Instance Attribute Summary

Attributes inherited from MCypher

#init

Instance Method Summary collapse

Constructor Details

#initializeRAES

Returns a new instance of RAES.



15
16
17
# File 'lib/reedb/security/aes.rb', line 15

def initialize
  super # => Super constructor
end

Instance Method Details

#decrypt(data) ⇒ Object

Decrypt the cypher text using the encryption key Returns the original clear text. Throws exceptions



61
62
63
64
65
66
67
68
# File 'lib/reedb/security/aes.rb', line 61

def decrypt(data)
  begin
    return AES.decrypt(data, @key) unless @key.nil?
  rescue Exception => e
    puts e.message
    raise DecryptionFailedError.new, "An error was encountered while decrypting data"
  end
end

#encrypt(data) ⇒ Object

Encrypt the clear text using the encryption key Returns a base64 encoded string Throws exceptions



48
49
50
51
52
53
54
55
# File 'lib/reedb/security/aes.rb', line 48

def encrypt(data)
  begin
    return AES.encrypt(data, @key) unless @key.nil?
  rescue Exception => e
    puts e.message
    raise EncryptionFailedError.new, "An error was encountered while encrypting data"
  end
end

#finalise_shift(fresh) ⇒ Object

Returns new encrypted key



85
86
87
88
89
90
91
# File 'lib/reedb/security/aes.rb', line 85

def finalise_shift(fresh)
  @key = @tmp_key # => Finishing the cipher shift
  key_encrypted = AES.encrypt(@tmp_key, fresh)
  remove_instance_variable(:@tmp_key)  # => Removing insecure imprint

  return key_encrypted # => To be stored in the new config file!
end

#init_shiftObject

Starts the shift of the main password and creates a new key (cipher) to encrypt with the new pw



72
73
74
# File 'lib/reedb/security/aes.rb', line 72

def init_shift
  @tmp_key = AES.key
end

#shift_cipher(file) ⇒ Object

Change the encryption cipher for a file in the vault.



78
79
80
81
# File 'lib/reedb/security/aes.rb', line 78

def shift_cipher(file)
  temp = AES.decrypt(file, @key) unless @key.nil?
  return AES.encrypt(temp, @tmp_key)
end

#start_encryption(password, raw_key = nil) ⇒ Object

Starts the encryption and loads a key by either generating a new one or loading an encrypted one from file.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/reedb/security/aes.rb', line 22

def start_encryption(password, raw_key = nil)
  if raw_key != nil
    # => Decrypting key with user password
    @key = AES.decrypt(raw_key, password)
  else
    # => Generating new key and encrypting it with user pw
    @key = AES.key
    key_encrypted = AES.encrypt(@key, password)
  end
  
  # => At this point @key should be the unencrypted key!
  @init = true
  return key_encrypted
end

#stop_encryptionObject

Tries to remove the unencryted key from memory as best as possible. Stops the encryption and prevents further decrypts to occur.



39
40
41
42
# File 'lib/reedb/security/aes.rb', line 39

def stop_encryption
  remove_instance_variable(:@key)
  @init = false
end