Class: PasswordVault::Vault

Inherits:
Object
  • Object
show all
Defined in:
lib/password_vault/vault.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Vault

Returns a new instance of Vault.



6
7
8
# File 'lib/password_vault/vault.rb', line 6

def initialize(key)
  @key = Digest::SHA512.digest(key)
end

Instance Method Details

#delete_password(name) ⇒ Object



19
20
21
22
23
# File 'lib/password_vault/vault.rb', line 19

def delete_password(name)
  if p = get_password(name)
    @passwords -= [p]
  end
end

#dumpObject



42
43
44
# File 'lib/password_vault/vault.rb', line 42

def dump
  Marshal.dump(passwords)
end

#get_password(name) ⇒ Object



14
15
16
17
# File 'lib/password_vault/vault.rb', line 14

def get_password(name)
  found = @passwords.select {|p| p.name == name }
  found.first
end

#lockObject



33
34
35
# File 'lib/password_vault/vault.rb', line 33

def lock
  crypt(:encrypt, dump)
end

#passwordsObject



10
11
12
# File 'lib/password_vault/vault.rb', line 10

def passwords
  @passwords ||= []
end

#readObject



29
30
31
# File 'lib/password_vault/vault.rb', line 29

def read
  unlock File.read(PasswordVault::VaultFile)
end

#unlock(data) ⇒ Object



37
38
39
40
# File 'lib/password_vault/vault.rb', line 37

def unlock(data)
  dump = crypt(:decrypt, data)
  @passwords = Marshal.load(dump)
end

#writeObject



25
26
27
# File 'lib/password_vault/vault.rb', line 25

def write
  File.open(PasswordVault::VaultFile, 'w') {|f| f.write(lock) }
end