class Trustworthy::Settings

Public Class Methods

new(store) click to toggle source
# File lib/trustworthy/settings.rb, line 12
def initialize(store)
  @store = store
end
open(filename) { |settings| ... } click to toggle source
# File lib/trustworthy/settings.rb, line 3
def self.open(filename)
  store = YAML::Store.new(filename)
  store.ultra_safe = true if store.respond_to?(:ultra_safe=)

  store.transaction do
    yield Trustworthy::Settings.new(store)
  end
end

Public Instance Methods

_cipher_from_password(salt, password) click to toggle source
# File lib/trustworthy/settings.rb, line 46
def _cipher_from_password(salt, password)
  cost, salt = salt.rpartition('$')
  key = SCrypt::Engine.scrypt(password, salt, cost, Trustworthy::Cipher.key_len)
  Trustworthy::Cipher.new(key)
end
_decrypt(ciphertext, salt, password) click to toggle source
# File lib/trustworthy/settings.rb, line 52
def _decrypt(ciphertext, salt, password)
  cipher = _cipher_from_password(salt, password)
  nonce, ciphertext = ciphertext.split('--').map do |field|
    Base64.decode64(field)
  end
  cipher.decrypt(nonce, '', ciphertext)
end
_encrypt(plaintext, salt, password) click to toggle source
# File lib/trustworthy/settings.rb, line 60
def _encrypt(plaintext, salt, password)
  cipher = _cipher_from_password(salt, password)
  nonce = Trustworthy::Cipher.generate_nonce
  ciphertext = cipher.encrypt(nonce, '', plaintext)
  [nonce, ciphertext].map do |field|
    Base64.strict_encode64(field)
  end.join('--')
end
add_key(key, username, password) click to toggle source
# File lib/trustworthy/settings.rb, line 16
def add_key(key, username, password)
  salt = SCrypt::Engine.generate_salt(Trustworthy::SCryptParams)
  encrypted_point = _encrypt(key.to_s, salt, password)
  @store[username] = {'salt' => salt, 'encrypted_point' => encrypted_point, 'timestamp' => DateTime.now.iso8601}
end
empty?() click to toggle source
# File lib/trustworthy/settings.rb, line 22
def empty?
  @store.roots.empty?
end
find_key(username) click to toggle source
# File lib/trustworthy/settings.rb, line 26
def find_key(username)
  @store[username]
end
key?(username) click to toggle source
# File lib/trustworthy/settings.rb, line 30
def key?(username)
  @store.root?(username)
end
recoverable?() click to toggle source
# File lib/trustworthy/settings.rb, line 34
def recoverable?
  @store.roots.count >= 2
end
unlock_key(username, password) click to toggle source
# File lib/trustworthy/settings.rb, line 38
def unlock_key(username, password)
  key = find_key(username)
  salt = key['salt']
  ciphertext = key['encrypted_point']
  plaintext = _decrypt(ciphertext, salt, password)
  Trustworthy::Key.create_from_string(plaintext)
end