class SecureFileStore
Public Class Methods
new(key)
click to toggle source
# File lib/secure_file_store.rb, line 6 def initialize(key) @key = unhex(key) end
Public Instance Methods
read_file(iv, file_path)
click to toggle source
# File lib/secure_file_store.rb, line 15 def read_file(iv, file_path) data = File.read(file_path) decrypt(@key, unhex(iv), data) end
read_settings(dir=".gitolemy")
click to toggle source
# File lib/secure_file_store.rb, line 26 def read_settings(dir=".gitolemy") file_path = Dir[File.join(dir, "config-*")].first iv = File.basename(file_path).gsub(/^config-/, "") JSON.parse(read_file(iv, file_path)) end
write_file(data, file_path)
click to toggle source
# File lib/secure_file_store.rb, line 10 def write_file(data, file_path) iv, data = encrypt(@key, data) File.open(file_path, "wb") { |file| file.write(data) } end
write_settings(settings, dir=".gitolemy")
click to toggle source
# File lib/secure_file_store.rb, line 20 def write_settings(settings, dir=".gitolemy") iv, data = encrypt(@key, settings.to_json()) file_path = File.join(dir, "config-#{hex(iv)}") File.open(file_path, "wb") { |file| file.write(data) } end
Private Instance Methods
decrypt(private_key, iv, data)
click to toggle source
# File lib/secure_file_store.rb, line 54 def decrypt(private_key, iv, data) cipher = OpenSSL::Cipher::AES.new(256, :CBC) cipher.decrypt cipher.key = private_key cipher.iv = iv cipher.update(data) + cipher.final end
encrypt(private_key, data)
click to toggle source
# File lib/secure_file_store.rb, line 45 def encrypt(private_key, data) cipher = OpenSSL::Cipher::AES.new(256, :CBC) cipher.encrypt cipher.key = private_key iv = cipher.random_iv out = cipher.update(data) + cipher.final [iv, out] end
hex(iv)
click to toggle source
# File lib/secure_file_store.rb, line 34 def hex(iv) iv.unpack("H*").first end
unhex(iv)
click to toggle source
# File lib/secure_file_store.rb, line 38 def unhex(iv) iv .scan(/../) .map { |x| x.hex } .pack("c*") end