Skip to content

Instantly share code, notes, and snippets.

@gorobey
Forked from jmazzi/lastpass_to_keepassx.rb
Created April 14, 2016 07:44
Show Gist options
  • Save gorobey/bfa4d34d8514401935357b59b30202c3 to your computer and use it in GitHub Desktop.
Save gorobey/bfa4d34d8514401935357b59b30202c3 to your computer and use it in GitHub Desktop.
Convert LastPass CSV to KeePassX XML
#!/usr/bin/ruby
#
# I recommend using Pocket to do the export. It works better than the browser extensions.
require 'rubygems'
require 'htmlentities'
require 'csv'
# CHANGE THIS
input_file = '/path/to/passwords.csv'
output_file = File.join(`pwd`.chomp, 'pass.xml')
coder = HTMLEntities.new
csv = CSV::parse(File.open(input_file) {|f| f.read} )
passwords = {}
csv.each do |record|
passwords[coder.encode(record[5])] ||= []
passwords[coder.encode(record[5])] << { :username => coder.encode(record[1]),
:password => coder.encode(record[2]),
:title => coder.encode(record[4]),
:comment => coder.encode(record[3]),
:url => coder.encode(record[0]) }
end
File.open(output_file, 'w') do |f|
f.puts '<!DOCTYPE KEEPASSX_DATABASE>'
f.puts '<database>'
passwords.keys.each do |key|
f.puts ' <group>'
f.puts " <title>#{key}</title>"
passwords[key].each do |password|
f.puts " <entry>"
f.puts " <title>#{password[:title]}</title>"
f.puts " <username>#{password[:username]}</username>"
f.puts " <password>#{password[:password]}</password>"
f.puts " <url>#{password[:url]}</url>"
f.puts " <comment>#{password[:comment]}</comment>"
f.puts " </entry>"
end
f.puts ' </group>'
end
f.puts '</database>'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment