Class: Cloudcrypt::SymmetricalCrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcrypt/symmetrical_crypt.rb

Constant Summary collapse

SYMMETRICAL_ALGORITHM =
'aes-256-cbc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file, destination_file) ⇒ SymmetricalCrypt

Returns a new instance of SymmetricalCrypt.



6
7
8
9
10
11
12
13
# File 'lib/cloudcrypt/symmetrical_crypt.rb', line 6

def initialize(source_file,destination_file)
    @source_file = source_file
    @destination_file = destination_file
    raise "#{@source_file} file doesn't exist" unless File.exists?(@source_file)
    raise "#{@destination_file} Already Exists" if File.exists?(@destination_file)
    
    @cipher = OpenSSL::Cipher::Cipher.new(SYMMETRICAL_ALGORITHM)
end

Instance Attribute Details

#random_ivObject (readonly)

Returns the value of attribute random_iv.



5
6
7
# File 'lib/cloudcrypt/symmetrical_crypt.rb', line 5

def random_iv
  @random_iv
end

#random_keyObject (readonly)

Returns the value of attribute random_key.



5
6
7
# File 'lib/cloudcrypt/symmetrical_crypt.rb', line 5

def random_key
  @random_key
end

Instance Method Details

#decrypt(key, iv) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/cloudcrypt/symmetrical_crypt.rb', line 25

def decrypt(key,iv)
    raise if ( key.empty? || iv.empty?)
    @cipher.decrypt # We are encypting
    @cipher.key = key
    @cipher.iv = iv
    write_to_disk
end

#encryptObject



15
16
17
18
19
20
21
22
23
# File 'lib/cloudcrypt/symmetrical_crypt.rb', line 15

def encrypt
    @cipher.encrypt # We are encypting
    # The OpenSSL library will generate random keys and IVs
    @random_key = @cipher.random_key
    @random_iv = @cipher.random_iv
    @cipher.key = @random_key
    @cipher.iv = @random_iv
    write_to_disk
end