module Keepassx::Database::Dumper

Public Instance Methods

save(opts = {}) click to toggle source

Save database to file storage

@param password [String] Password the database will be encoded with. @return [Fixnum]

# File lib/keepassx/database/dumper.rb, line 29
def save(opts = {})
  path     = opts.delete(:path) { nil }
  password = opts.delete(:password) { nil }

  new_path     = path || @path
  new_password = password || @password

  raise ArgumentError, 'File path is not set' if new_path.nil?
  raise ArgumentError, 'Password is not set' if new_password.nil?

  File.open(new_path, 'wb') do |file|
    file.write dump(new_password)
  end
end
to_a(opts = {}) click to toggle source

Dump Array representation of database.

@return [Array]

# File lib/keepassx/database/dumper.rb, line 10
def to_a(opts = {})
  result = []
  find_groups(level: 0).each { |group| result << build_branch(group, opts) }
  result
end
to_yaml(opts = {}) click to toggle source

Dump YAML representation of database.

@return [String]

# File lib/keepassx/database/dumper.rb, line 20
def to_yaml(opts = {})
  YAML.dump to_a(opts)
end

Private Instance Methods

build_branch(group, opts = {}) click to toggle source

Organize descendants in proper structure for given group. @param [Keepassx::Group] group Root group, branch is build for.

# File lib/keepassx/database/dumper.rb, line 65
def build_branch(group, opts = {})
  group_hash = group.to_hash(opts)

  group_entries = find_entries(group: group)
  descendant_groups = find_groups(parent: group)

  unless group_entries.nil?
    group_hash['entries'] = []
    group_entries.each { |e| group_hash['entries'] << e.to_hash(opts) }
  end

  unless descendant_groups.nil?
    group_hash['groups'] = []
    # Recursively build branch for descendant groups
    descendant_groups.each { |g| group_hash['groups'] << build_branch(g, opts) }
  end

  group_hash
end
dump(password) click to toggle source

Get raw encoded database.

@param password [String] Password the database will be encoded with. @param key_file [String] Path to key file. @return [String]

# File lib/keepassx/database/dumper.rb, line 53
def dump(password)
  final_key = header.final_key(password)
  initialize_payload
  header.content_hash = checksum
  @encrypted_payload  = encrypt_payload(@payload, final_key)
  data = header.encode << @encrypted_payload.to_s
  data
end