Created
May 28, 2020 19:32
-
-
Save knugie/52116f0f4af8f2e30851c3109fca82bc to your computer and use it in GitHub Desktop.
Encrypt, Decrypt large files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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