Module: Keepassx::Database::Loader

Included in:
Keepassx::Database
Defined in:
lib/keepassx/database/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



9
10
11
# File 'lib/keepassx/database/loader.rb', line 9

def entries
  @entries
end

#groupsObject (readonly)

Returns the value of attribute groups.



8
9
10
# File 'lib/keepassx/database/loader.rb', line 8

def groups
  @groups
end

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

Instance Method Details

#checksumString

Get actual payload checksum.

Returns:

  • (String)


78
79
80
# File 'lib/keepassx/database/loader.rb', line 78

def checksum
  Digest::SHA256.digest(payload)
end

#initialize(opts) ⇒ Object

rubocop:disable Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/keepassx/database/loader.rb', line 12

def initialize(opts)
  @password = nil
  @groups   = []
  @entries  = []
  raw_db    = ''

  if opts.is_a?(File)
    @path  = opts.path
    raw_db = read_file(opts)
    load_database(raw_db)

  elsif opts.is_a?(String)
    @path  = opts
    raw_db = read_file(opts) if File.exist?(opts)
    load_database(raw_db)

  elsif opts.is_a?(Array)
    @path = nil
    load_database(raw_db)
    opts.each { |item| parse_data(item) }
  end
end

#lengthFixnum

Get Entries and Groups total number.

Returns:

  • (Fixnum)


91
92
93
94
95
96
97
98
99
# File 'lib/keepassx/database/loader.rb', line 91

def length
  length = 0
  [@groups, @entries].each do |items|
    items.each do |item|
      length += item.length
    end
  end
  length
end

#payloadObject



83
84
85
# File 'lib/keepassx/database/loader.rb', line 83

def payload
  @payload ||= initialize_payload
end

#unlock(password, keyfile = nil) ⇒ Boolean

Unlock database.

rubocop:disable Metrics/MethodLength

Parameters:

  • password (String)

    Database password.

Returns:

  • (Boolean)

    Whether or not password validation successfull.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/keepassx/database/loader.rb', line 42

def unlock(password, keyfile = nil)
  return true unless locked?

  # Store password as we'll need it to dump/save database
  @password = password
  keyfile_data = keyfile ? read_file(keyfile) : nil

  # Uncrypt database
  final_key  = header.final_key(password, keyfile_data)
  @payload   = decrypt_payload(@encrypted_payload, final_key)
  payload_io = StringIO.new(@payload)

  # Load it
  groups   = Group.extract_from_payload(header, payload_io)
  @groups  = initialize_groups(groups)
  @entries = Entry.extract_from_payload(header, payload_io)

  # Make groups <-> entries association
  @entries.each do |entry|
    group = @groups.detect { |g| g.id == entry.group_id }
    group.entries << entry
    entry.group = group
  end

  @locked = false

  true
rescue OpenSSL::Cipher::CipherError
  false
end