class Gibberish::AES::CBC

Constants

BUFFER_SIZE

Attributes

cipher[R]
password[R]
size[R]

Public Class Methods

new(password, size=256, mode="cbc") click to toggle source

Initialize with the password

@param [String] password @param [Integer] size @param [String] mode

# 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

Public Instance Methods

d(data, opts={})
Alias for: decrypt
dec(data, opts={})
Alias for: decrypt
dec_file(from_file, to_file)
Alias for: decrypt_file
decrypt(data, opts={}) click to toggle source
# 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
Also aliased as: dec, d
decrypt_file(from_file, to_file) click to toggle source
# 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
Also aliased as: dec_file, df
decrypt_stream(in_stream, out_stream) click to toggle source
# 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
df(from_file, to_file)
Alias for: decrypt_file
e(data, opts={})
Alias for: encrypt
ef(from_file, to_file, opts={})
Alias for: encrypt_file
enc(data, opts={})
Alias for: encrypt
enc_file(from_file, to_file, opts={})
Alias for: encrypt_file
encrypt(data, opts={}) click to toggle source
# 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
Also aliased as: enc, e
encrypt_file(from_file, to_file, opts={}) click to toggle source
# 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
Also aliased as: enc_file, ef
encrypt_stream(in_stream, out_stream, opts={}) click to toggle source
# 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

Private Instance Methods

copy_stream(in_stream, out_stream) click to toggle source
# File lib/gibberish/aes.rb, line 311
def copy_stream(in_stream, out_stream)
  buf = ''
  while in_stream.read(BUFFER_SIZE, buf)
    out_stream << cipher.update(buf)
  end
  out_stream << cipher.final
  out_stream.flush
end
generate_salt(supplied_salt) click to toggle source
# File lib/gibberish/aes.rb, line 297
def generate_salt(supplied_salt)
  if supplied_salt
    return supplied_salt.to_s[0,8].ljust(8,'.')
  end
  s = ''
  8.times {s << rand(255).chr}
  s
end
setup_cipher(method, salt) click to toggle source
# File lib/gibberish/aes.rb, line 306
def setup_cipher(method, salt)
  cipher.send(method)
  cipher.pkcs5_keyivgen(password, salt, 1)
end