class Parol::Database

Public Class Methods

new(database, password) click to toggle source
# File lib/parol/database.rb, line 11
def initialize database, password
  @database = database
  @password = password

  @binary_password = String.new @password, encoding: 'BINARY'
  @box = RbNaCl::SimpleBox.from_secret_key @binary_password

  rescue RbNaCl::LengthError
    raise BadPasswordLength
end

Public Instance Methods

accounts() { |h| ... } click to toggle source
# File lib/parol/database.rb, line 35
def accounts &block
  data = decrypt

  if block_given?
    data.each do |h|
      yield h
    end
  else
    data
  end
end
add(program, login, password, notes) click to toggle source
# File lib/parol/database.rb, line 22
def add program, login, password, notes
  data = decrypt

  data << {
      program: program,
      login: login,
      password: password,
      notes: notes
  }

  encrypt data
end
save(data) click to toggle source
# File lib/parol/database.rb, line 47
def save data
  encrypt data
end

Private Instance Methods

create() click to toggle source
# File lib/parol/database.rb, line 71
def create
  File.open @database, 'wb' do |file|
    dump = Marshal.dump Array.new
    file.write @box.encrypt(dump)
  end
end
decrypt() click to toggle source
# File lib/parol/database.rb, line 52
def decrypt
  unless File.exist? @database
    create
  end

  data = File.binread @database
  Marshal.load @box.decrypt(data)

rescue RbNaCl::CryptoError
  raise DecryptionFailed
end
encrypt(data) click to toggle source
# File lib/parol/database.rb, line 64
def encrypt data
  File.open @database, 'wb' do |file|
    dump = Marshal.dump data
    file.write @box.encrypt dump
  end
end