Class: Underlock::FileEncryptor
- Inherits:
-
Object
- Object
- Underlock::FileEncryptor
- Defined in:
- lib/underlock/file_encryptor.rb
Instance Method Summary collapse
Instance Method Details
#decrypt(encrypted_entity) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/underlock/file_encryptor.rb', line 30 def decrypt(encrypted_entity) decode_cipher = Underlock::Base.config.cipher.dup decode_cipher.decrypt decode_cipher.key = private_decrypt(encrypted_entity.key) decode_cipher.iv = private_decrypt(encrypted_entity.iv) @base_dir, @filename = File.split(encrypted_entity.encrypted_file) File.open(decrypted_filepath, 'wb') do |decrypted_file| File.open(encrypted_entity.encrypted_file, 'rb') do |inf| loop do r = inf.read(4096) break unless r decrypted_file << decode_cipher.update(r) end end end File.new(decrypted_filepath) end |
#encrypt(file) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/underlock/file_encryptor.rb', line 4 def encrypt(file) file = File.realpath(file) @base_dir, @filename = File.split(file) cipher = Underlock::Base.config.cipher.dup cipher.encrypt key = cipher.random_key iv = cipher.random_iv File.open(encrypted_filepath, "wb") do |encrypted_file| File.open(file, 'rb') do |inf| loop do r = inf.read(4096) break unless r encrypted_file << cipher.update(r) end end encrypted_file << cipher.final end encrypted_file = File.new(encrypted_filepath) encrypted_key = public_encrypt(key) encrypted_iv = public_encrypt(iv) EncryptedEntity.new(encrypted_file: encrypted_file, key: encrypted_key, iv: encrypted_iv) end |