Skip to content

Instantly share code, notes, and snippets.

@knugie
Created May 28, 2020 19:32
Show Gist options
  • Save knugie/52116f0f4af8f2e30851c3109fca82bc to your computer and use it in GitHub Desktop.
Save knugie/52116f0f4af8f2e30851c3109fca82bc to your computer and use it in GitHub Desktop.
Encrypt, Decrypt large files
# From https://tjay.dev/howto-working-efficiently-with-large-files-in-ruby/
# Encrypt
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
cipher.key = KEY
cipher.iv = IV
file = nil
enc_file = nil
profile do
buf = ""
file = File.open("large_1G.txt", "rb")
enc_file = File.open("large_1G.txt.enc", "wb")
while buf = file.read(4096)
enc_file << cipher.update(buf)
end
enc_file << cipher.final
end
file.close
enc_file.close
# Decrypt
decipher = OpenSSL::Cipher::AES256.new(:CBC)
decipher.decrypt
decipher.key = KEY
decipher.iv = IV
dec_file = nil
enc_file = nil
profile do
buf = ""
enc_file = File.open("large_1G.txt.enc", "rb")
dec_file = File.open("large_1G.txt.dec", "wb")
while buf = enc_file.read(4096)
dec_file << decipher.update(buf)
end
dec_file << decipher.final
end
dec_file.close
enc_file.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment