Class: Keepassx::Database

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_db) ⇒ Database

Returns a new instance of Database.



11
12
13
14
# File 'lib/keepassx/database.rb', line 11

def initialize(raw_db)
  @header = Header.new(raw_db[0..124])
  @encrypted_payload = raw_db[124..-1]
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



4
5
6
# File 'lib/keepassx/database.rb', line 4

def entries
  @entries
end

#groupsObject (readonly)

Returns the value of attribute groups.



4
5
6
# File 'lib/keepassx/database.rb', line 4

def groups
  @groups
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/keepassx/database.rb', line 4

def header
  @header
end

Class Method Details

.open(path) ⇒ Object



6
7
8
9
# File 'lib/keepassx/database.rb', line 6

def self.open(path)
  content = File.respond_to?(:binread) ? File.binread(path) : File.read(path)
  self.new(content)
end

Instance Method Details

#decrypt_payloadObject



41
42
43
# File 'lib/keepassx/database.rb', line 41

def decrypt_payload
  @payload = AESCrypt.decrypt(@encrypted_payload, @final_key, header.encryption_iv, 'AES-256-CBC')
end

#entry(title) ⇒ Object



16
17
18
# File 'lib/keepassx/database.rb', line 16

def entry(title)
  @entries.detect { |e| e.title == title }
end

#search(pattern) ⇒ Object



31
32
33
34
35
# File 'lib/keepassx/database.rb', line 31

def search(pattern)
  backup = groups.detect { |g| g.name == "Backup" }
  backup_group_id = backup && backup.group_id
  entries.select { |e| e.group_id != backup_group_id && e.title =~ /#{pattern}/i }
end

#unlock(master_password) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/keepassx/database.rb', line 20

def unlock(master_password)
  @final_key = header.final_key(master_password)
  decrypt_payload
  payload_io = StringIO.new(@payload)
  @groups = Group.extract_from_payload(header, payload_io)
  @entries = Entry.extract_from_payload(header, payload_io)
  true
rescue OpenSSL::Cipher::CipherError
  false
end

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/keepassx/database.rb', line 37

def valid?
  @header.valid?
end