Class: Gibberish::AES::CBC

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

Constant Summary collapse

BUFFER_SIZE =
4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password, size = 256, mode = "cbc") ⇒ CBC

Initialize with the password

Parameters:

  • password (String)
  • size (Integer) (defaults to: 256)
  • mode (String) (defaults to: "cbc")


218
219
220
221
222
223
# File 'lib/gibberish/aes.rb', line 218

def initialize(password, size=256, mode="cbc")
  @password = password
  @size = size
  @mode = mode
  @cipher = OpenSSL::Cipher.new("aes-#{size}-#{mode}")
end

Instance Attribute Details

#cipherObject (readonly)

Returns the value of attribute cipher.



211
212
213
# File 'lib/gibberish/aes.rb', line 211

def cipher
  @cipher
end

#passwordObject (readonly)

Returns the value of attribute password.



211
212
213
# File 'lib/gibberish/aes.rb', line 211

def password
  @password
end

#sizeObject (readonly)

Returns the value of attribute size.



211
212
213
# File 'lib/gibberish/aes.rb', line 211

def size
  @size
end

Instance Method Details

#decrypt(data, opts = {}) ⇒ Object Also known as: dec, d

Raises:

  • (ArgumentError)


235
236
237
238
239
240
241
242
# File 'lib/gibberish/aes.rb', line 235

def decrypt(data, opts={})
  raise ArgumentError, 'Data is too short' unless data.length >= 16
  data = Base64.decode64(data) unless opts[:binary]
  salt = data[8..15]
  data = data[16..-1]
  setup_cipher(:decrypt, salt)
  cipher.update(data) + cipher.final
end

#decrypt_file(from_file, to_file) ⇒ Object Also known as: dec_file, df



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/gibberish/aes.rb', line 263

def decrypt_file(from_file, to_file)
  buf = ""
  salt = ""
  File.open(to_file, "wb") do |outf|
    File.open(from_file, "rb") do |inf|
      inf.seek(8, IO::SEEK_SET)
      inf.read(8, salt)
      setup_cipher(:decrypt, salt)
      while inf.read(4096, buf)
        outf << self.cipher.update(buf)
      end
      outf << self.cipher.final
    end
  end
end

#decrypt_stream(in_stream, out_stream) ⇒ Object



288
289
290
291
292
293
# File 'lib/gibberish/aes.rb', line 288

def decrypt_stream(in_stream, out_stream)
  header = in_stream.read(16)
  salt = header[8..15]
  setup_cipher(:decrypt, salt)
  copy_stream in_stream, out_stream
end

#encrypt(data, opts = {}) ⇒ Object Also known as: enc, e



225
226
227
228
229
230
231
# File 'lib/gibberish/aes.rb', line 225

def encrypt(data, opts={})
  salt = generate_salt(opts[:salt])
  setup_cipher(:encrypt, salt)
  e = cipher.update(data) + cipher.final
  e = "Salted__#{salt}#{e}" #OpenSSL compatible
  opts[:binary] ? e : Base64.encode64(e)
end

#encrypt_file(from_file, to_file, opts = {}) ⇒ Object Also known as: enc_file, ef



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/gibberish/aes.rb', line 246

def encrypt_file(from_file, to_file, opts={})
  salt = generate_salt(opts[:salt])
  setup_cipher(:encrypt, salt)
  buf = ""
  File.open(to_file, "wb") do |outf|
    outf << "Salted__#{salt}"
    File.open(from_file, "rb") do |inf|
      while inf.read(4096, buf)
        outf << self.cipher.update(buf)
      end
      outf << self.cipher.final
    end
  end
end

#encrypt_stream(in_stream, out_stream, opts = {}) ⇒ Object



281
282
283
284
285
286
# File 'lib/gibberish/aes.rb', line 281

def encrypt_stream(in_stream, out_stream, opts={})
  salt = generate_salt(opts[:salt])
  setup_cipher(:encrypt, salt)
  out_stream << "Salted__#{salt}"
  copy_stream in_stream, out_stream
end