Class: Keyrack::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/keyrack/database.rb

Constant Summary collapse

DEFAULT_ENCRYPT_OPTIONS =
{ :maxmem => 0, :maxmemfrac => 0.125, :maxtime => 5.0 }
DEFAULT_DECRYPT_OPTIONS =
{ :maxmem => 0, :maxmemfrac => 0.500, :maxtime => 20.0 }
VERSION =
4

Instance Method Summary collapse

Constructor Details

#initialize(password, store, encrypt_options = {}, decrypt_options = {}) ⇒ Database

Returns a new instance of Database.



7
8
9
10
11
12
13
14
15
# File 'lib/keyrack/database.rb', line 7

def initialize(password, store, encrypt_options = {}, decrypt_options = {})
  @dirty = false
  @encrypt_options = DEFAULT_ENCRYPT_OPTIONS.merge(encrypt_options)
  @decrypt_options = DEFAULT_DECRYPT_OPTIONS.merge(decrypt_options)
  @store = store
  @password = password_hash(password)
  @attributes = decrypt(password)
  setup_hooks
end

Instance Method Details

#change_password(current_password, new_password) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/keyrack/database.rb', line 40

def change_password(current_password, new_password)
  if password_hash(current_password) == @password
    @password = password_hash(new_password)
    true
  else
    false
  end
end

#dirty?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/keyrack/database.rb', line 25

def dirty?
  @dirty
end

#save(password) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/keyrack/database.rb', line 29

def save(password)
  if password_hash(password) == @password
    @store.write(Scrypty.encrypt(JSON.generate(self.to_h), password,
      *@encrypt_options.values_at(:maxmem, :maxmemfrac, :maxtime)))
    @dirty = false
    true
  else
    false
  end
end

#to_hObject



49
50
51
52
53
54
55
56
# File 'lib/keyrack/database.rb', line 49

def to_h
  hash = @attributes.dup
  hash['groups'] = hash['groups'].inject({}) do |hash2, (key, value)|
    hash2[key] = value.to_h
    hash2
  end
  hash
end

#top_groupObject



21
22
23
# File 'lib/keyrack/database.rb', line 21

def top_group
  @attributes['groups']['top']
end

#versionObject



17
18
19
# File 'lib/keyrack/database.rb', line 17

def version
  @attributes['version']
end