1 | #!/usr/bin/python |
---|
2 | """This script will take the plaintext export file from MyPasswordSafe (named |
---|
3 | 'mypasswordsafe.txt' in the current directory) and convert it to the CSV import |
---|
4 | format expected by OI Safe (for the Android phone platform) and write it to |
---|
5 | 'oisafe.csv'. |
---|
6 | """ |
---|
7 | |
---|
8 | class PasswordData(object): |
---|
9 | """Holds all information about the passwords. |
---|
10 | |
---|
11 | Can load from MyPasswordSafe exports, and save to a form OISafe can import. |
---|
12 | """ |
---|
13 | def __init__(self): |
---|
14 | self.master_password = None |
---|
15 | self.passwords = None |
---|
16 | |
---|
17 | def load_mps(self, filename): |
---|
18 | """Expects a plain text export from mypasswordsafe as input |
---|
19 | That file is kind of a tab-delimitted file, with the master password as |
---|
20 | the only content of the first line. |
---|
21 | The fields are: |
---|
22 | name, user, password, created, modified, accessed, lifetime, uuid |
---|
23 | notes |
---|
24 | |
---|
25 | Where the notes field is the content of the second line, with \n indicating newlines. |
---|
26 | """ |
---|
27 | lines = open(filename).readlines() |
---|
28 | self.master_password = lines[:-1] |
---|
29 | self.passwords = [] |
---|
30 | field_names = ['name', 'user', 'password', 'group', 'created', |
---|
31 | 'modified', 'accessed', 'lifetime', 'uuid'] |
---|
32 | for i in range(1, len(lines), 2): |
---|
33 | entry = {} |
---|
34 | fields = lines[i].split('\t') |
---|
35 | entry['notes'] = lines[i+1].replace('\\n', '\n') |
---|
36 | for fname, fvalue in zip(field_names, fields): |
---|
37 | #print fname, fvalue |
---|
38 | entry[fname] = fvalue |
---|
39 | self.passwords.append(entry) |
---|
40 | |
---|
41 | def save_oisafe(self, filename): |
---|
42 | """Writes out the password information stored in this object to the |
---|
43 | named file in a format that OI Safe can import. |
---|
44 | """ |
---|
45 | out = open(filename, 'w') |
---|
46 | out.write('"Category","Description","Website","Username","Password",' |
---|
47 | '"Notes"\n') |
---|
48 | for entry in self.passwords: |
---|
49 | category = csv_escape_quotes(entry['group'].lstrip('/')) |
---|
50 | description = csv_escape_quotes(entry['name']) |
---|
51 | website = '' |
---|
52 | username = csv_escape_quotes(entry['user']) |
---|
53 | password = csv_escape_quotes(entry['password']) |
---|
54 | full_notes = entry['notes'].strip('\n') + '\n\n' |
---|
55 | for extra_field in ['created', 'modified', 'accessed', 'lifetime', |
---|
56 | 'uuid']: |
---|
57 | full_notes += '%s: %s\n' % (extra_field, entry[extra_field]) |
---|
58 | notes = csv_escape_quotes(full_notes.rstrip('\n')) |
---|
59 | |
---|
60 | line = '","'.join([category, description, website, username, |
---|
61 | password, notes]) |
---|
62 | out.write('"%s"\n' % line) |
---|
63 | |
---|
64 | |
---|
65 | def csv_escape_quotes(text): |
---|
66 | """Quote the " characters in the provided text by doubling them. |
---|
67 | """ |
---|
68 | return text.replace('"', '""') |
---|
69 | |
---|
70 | |
---|
71 | def main(): |
---|
72 | """Converts a MyPasswordSafe export to an OI Safe import. |
---|
73 | """ |
---|
74 | database = PasswordData() |
---|
75 | database.load_mps('mypasswordsafe.txt') |
---|
76 | database.save_oisafe('oisafe.csv') |
---|
77 | |
---|
78 | |
---|
79 | if __name__ == '__main__': |
---|
80 | main() |
---|
81 | |
---|