Class: Gibberish::AES::CBC

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/aes_crypt.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")


143
144
145
146
147
148
# File 'lib/ext/aes_crypt.rb', line 143

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

Instance Attribute Details

#cipherObject (readonly)

Returns the value of attribute cipher.



136
137
138
# File 'lib/ext/aes_crypt.rb', line 136

def cipher
  @cipher
end

#passwordObject (readonly)

Returns the value of attribute password.



136
137
138
# File 'lib/ext/aes_crypt.rb', line 136

def password
  @password
end

#sizeObject (readonly)

Returns the value of attribute size.



136
137
138
# File 'lib/ext/aes_crypt.rb', line 136

def size
  @size
end

Instance Method Details

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

Raises:

  • (ArgumentError)


160
161
162
163
164
165
166
167
# File 'lib/ext/aes_crypt.rb', line 160

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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ext/aes_crypt.rb', line 188

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



213
214
215
216
217
218
# File 'lib/ext/aes_crypt.rb', line 213

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



150
151
152
153
154
155
156
# File 'lib/ext/aes_crypt.rb', line 150

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ext/aes_crypt.rb', line 171

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



206
207
208
209
210
211
# File 'lib/ext/aes_crypt.rb', line 206

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