Class: Filenc
- Inherits:
-
Object
- Object
- Filenc
- Defined in:
- lib/filenc.rb
Instance Attribute Summary collapse
-
#algo ⇒ Object
readonly
Returns the value of attribute algo.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#iv ⇒ Object
readonly
Returns the value of attribute iv.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#opt ⇒ Object
readonly
Returns the value of attribute opt.
Instance Method Summary collapse
- #dec ⇒ Object
- #enc ⇒ Object
-
#initialize(o) ⇒ Filenc
constructor
A new instance of Filenc.
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
#algo ⇒ Object (readonly)
Returns the value of attribute algo.
6 7 8 |
# File 'lib/filenc.rb', line 6 def algo @algo end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
6 7 8 |
# File 'lib/filenc.rb', line 6 def file @file end |
#iv ⇒ Object (readonly)
Returns the value of attribute iv.
6 7 8 |
# File 'lib/filenc.rb', line 6 def iv @iv end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/filenc.rb', line 6 def key @key end |
#opt ⇒ Object (readonly)
Returns the value of attribute opt.
6 7 8 |
# File 'lib/filenc.rb', line 6 def opt @opt end |
Instance Method Details
#dec ⇒ Object
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 |
#enc ⇒ Object
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 |