Sha256: a0e985095975b6b25f85a71621ba895d7ac4c9f8eae50a7426ce085b7fc70421
Contents?: true
Size: 1.43 KB
Versions: 1
Compression:
Stored size: 1.43 KB
Contents
# frozen_string_literal: true class EncryptedKeystore require 'openssl' require 'base64' attr_accessor :file, :out, :key, :iv def self.encrypt(file, out) enc = new(file: file, out: out) enc.encrypt { key: enc.key, iv: enc.iv } end def self.decrypt(file, out, key, iv) enc = new(file: file, out: out, key: key, iv: iv) enc.decrypt end def initialize(file: nil, out: nil, key: nil, iv: nil) @file = file @out = out @key = key @iv = iv end def decrypt validate write(dec_cipher) end def encrypt validate(enc: true) write(enc_cipher) end private def dec_cipher cipher = OpenSSL::Cipher.new('aes-256-cbc') cipher.decrypt cipher.key = Base64.decode64(@key) cipher.iv = Base64.decode64(@iv) cipher end def enc_cipher cipher = OpenSSL::Cipher.new('aes-256-cbc') cipher.encrypt @key = Base64.encode64(cipher.random_key) @iv = Base64.encode64(cipher.random_iv) cipher end def write(cipher) validate buffer = +'' File.open(@out, 'wb') do |outfile| File.open(@file, 'rb') do |infile| outfile << cipher.update(buffer) while infile.read(4096, buffer) outfile << cipher.final end end @out end def validate(enc: false) return true if !@file.nil? && !@out.nil? && (!enc || (!@key.nil? && !@iv.nil?)) raise ArgumentError, 'Required arguments have not been provided.' end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
encrypted-keystore-0.0.4 | lib/encrypted_keystore.rb |