Class: Filenc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ Filenc

Returns a new instance of Filenc.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/filenc.rb', line 8

def initialize(o)
  # Set Strong phrase value for @key > 32 and @iv > 16
  @key = ''
  @iv = ''
  # Default Algorithm set to AES-256-CBC
  @algo = 'AES-256-CBC'
  @file = o[:file]
  @opt = o
  raise "initialize @key and @iv values (@iv>15,@key>31 for AES-256-CBS)" if @key == '' or @iv == '' 
  raise "initialize @iv>15 & @key>31 for AES-256-CBS" if @algo == 'AES-256-CBC' and ( @key.length < 32 or @iv.length < 16 )
end

Instance Attribute Details

#algoObject (readonly)

Returns the value of attribute algo.



6
7
8
# File 'lib/filenc.rb', line 6

def algo
  @algo
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/filenc.rb', line 6

def file
  @file
end

#ivObject (readonly)

Returns the value of attribute iv.



6
7
8
# File 'lib/filenc.rb', line 6

def iv
  @iv
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/filenc.rb', line 6

def key
  @key
end

#optObject (readonly)

Returns the value of attribute opt.



6
7
8
# File 'lib/filenc.rb', line 6

def opt
  @opt
end

Instance Method Details

#decObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/filenc.rb', line 42

def dec
  puts "decrypting file '#{file}'"
  aes = OpenSSL::Cipher::Cipher.new(algo)
  aes.decrypt
  aes.key = key
  aes.iv = iv
  decfile = "#{file}.dec"
  File.open(decfile,'w') do |dec|
    File.open(file) do |f|
      loop do 
        r = f.read(4096) 
        break unless r        
        cipher = aes.update(r)       
        dec << cipher                       
      end                                        
    end          
    dec << aes.final
  end
  File.chown(opt[:userid],opt[:groupid],decfile)
  puts "decrypted to file '#{decfile}'"
end

#encObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/filenc.rb', line 20

def enc
  puts "encrypting file '#{file}'"
  aes = OpenSSL::Cipher::Cipher.new(algo)
  aes.encrypt
  aes.key = key
  aes.iv = iv
  encfile = "#{file}.enc"
  File.open(encfile,'w') do |enc|
    File.open(file) do |f|
      loop do
        r = f.read(4096)
        break unless r
        cipher = aes.update(r)
        enc << cipher
      end
    end
    enc << aes.final
  end
  File.chown(opt[:userid],opt[:groupid],encfile)
  puts "encrypted to file '#{encfile}'"
end