Skip to content

Instantly share code, notes, and snippets.

@revis0r
Created June 20, 2011 18:02
Show Gist options
  • Save revis0r/1036140 to your computer and use it in GitHub Desktop.
Save revis0r/1036140 to your computer and use it in GitHub Desktop.
def AES_encrypt(password)
if not_encrypted?
AES_crypt(password) { |c| c.encrypt }
encrypt!
end
end
def AES_decrypt(password)
if encrypted?
AES_crypt(password) { |c| c.decrypt }
decrypt!
end
end
private
def set_file_attributes
self.size = File.size(absolute_path)
end
def AES_crypt(password)
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
yield c
c.key = Digest::SHA1.hexdigest("abc" + password)
c.iv = Digest::SHA1.hexdigest(password)
output = Tempfile.new('slot', :external_encoding => Encoding::ASCII_8BIT)
File.open(self.absolute_path,'r') do |file|
while buf = file.read(4096)
output.write(c.update(buf))
end
file.close
end
output.write(c.final)
output.close
File.unlink self.absolute_path
FileUtils.move output.path, self.absolute_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment