forked from jorabin/KeePassJava2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickStart.java
206 lines (178 loc) · 7.98 KB
/
QuickStart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright 2015 Jo Rabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.linguafranca.pwdb.kdbx;
import org.junit.BeforeClass;
import org.linguafranca.pwdb.*;
import org.linguafranca.pwdb.kdb.KdbCredentials;
import org.linguafranca.pwdb.kdb.KdbDatabase;
import org.linguafranca.pwdb.kdbx.dom.DomDatabaseWrapper;
import org.linguafranca.pwdb.security.Encryption;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.linguafranca.util.TestUtil.getTestPrintStream;
/**
* Examples for QuickStart
*
* @author jo
*/
@SuppressWarnings("WeakerAccess")
public abstract class QuickStart<D extends Database<D, G, E, I>, G extends Group<D, G, E, I>, E extends Entry<D, G, E, I>, I extends Icon> {
static PrintStream printStream = getTestPrintStream();
public abstract D getDatabase();
public abstract D loadDatabase(Credentials creds, InputStream inputStream);
@BeforeClass
public static void ensureOutputDir() throws IOException {
Files.createDirectories(Paths.get("testOutput"));
}
/**
* Load KDBX
*/
public void loadKdbx() throws IOException {
// get an input stream
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("test123.kdbx")) {
// password credentials
Credentials credentials = new KdbxCreds("123".getBytes());
// Jaxb implementation seems a lot faster than the DOM implementation
D database = loadDatabase(credentials, inputStream);
// visit all groups and entries and list them to console
database.visit(new Visitor.Print(printStream));
}
}
/**
* Save KDBX
*/
private E entryFactory(D database, String s, int e) {
return database.newEntry(String.format("Group %s Entry %d", s, e));
}
public void saveKdbx() throws IOException {
// create an empty database
D database = getDatabase();
// add some groups and entries
for (Integer g = 0; g < 5; g++) {
G group = database.getRootGroup().addGroup(database.newGroup(g.toString()));
for (int e = 0; e <= g; e++) {
// entry factory is a local helper to populate an entry
group.addEntry(entryFactory(database, g.toString(), e));
}
}
// save to a file with password "123"
try (FileOutputStream outputStream = new FileOutputStream("testOutput/test.kdbx")) {
database.save(new KdbxCreds("123".getBytes()), outputStream);
}
}
/**
* Splice - add a group from one database to a parent from another (or copy from the same db)
*/
public void splice(G newParent, Group<?, ?, ?, ?> groupToSplice) {
G addedGroup = newParent.addGroup(newParent.getDatabase().newGroup(groupToSplice));
addedGroup.copy(groupToSplice);
}
/**
* Group by title
*/
public G groupByTitle(D database) {
List<? extends E> entries = database.findEntries(new Entry.Matcher() {
@Override
public boolean matches(Entry entry) {
return entry.getProperty(Entry.STANDARD_PROPERTY_NAME_TITLE).toLowerCase().contains("findme!");
}
});
// create a new group using DB factory method
G newParent = database.newGroup("Found entries");
// iterate over the found entries
for (E entry : entries) {
// copy the entry using DB factory so that it remains where it was found as well as being in new group
E copy = database.newEntry(entry);
// add new entry to new group
newParent.addEntry(copy);
}
return newParent;
}
/**
* Load KDB and save as KDBX
*/
public void loadKdb() throws IOException {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("test123.kdb");
// password credentials
Credentials credentials = new KdbCredentials.Password("123".getBytes());
// load KdbDatabase
KdbDatabase database = KdbDatabase.load(credentials, inputStream);
// visit all groups and entries and list them to console
// create a KDBX (database
D kdbxDatabase = getDatabase();
kdbxDatabase.setName("New Database");
kdbxDatabase.setDescription("Migration of KDB Database to KDBX Database");
// deep copy from group (not including source group, KDB database has simulated root)
kdbxDatabase.getRootGroup().copy(database.getRootGroup());
// save it
try (FileOutputStream f = new FileOutputStream("testOutput/migration.kdbx")) {
kdbxDatabase.save(new KdbxCreds("123".getBytes()), f);
}
}
/**
* Load KDBX V3 save as KDBX V4
*/
public void loadKdbx3SaveKdbx4(String resourceName, byte[] password, OutputStream v4OutputStream) throws IOException {
DomDatabaseWrapper database;
// password credentials
KdbxCreds credentials = new KdbxCreds(password);
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceName)) {
// load KdbDatabase
database = DomDatabaseWrapper.load(credentials, inputStream);
}
// create a KDBX (database
D kdbxDatabase = getDatabase();
kdbxDatabase.setName("New Database");
kdbxDatabase.setDescription("Migration of KDBX 3 Database to KDBX 4 Database");
// deep copy from group (not including source group, KDB database has simulated root)
kdbxDatabase.getRootGroup().copy(database.getRootGroup());
// choose a stream format - V4 Kdbx and choose some algorithms
KdbxHeader kdbxHeader = new KdbxHeader(KdbxHeader.KdbxHeaderOpts.V4_AES_ARGON_CHA_CHA);
KdbxStreamFormat formatV4 = new KdbxStreamFormat(kdbxHeader);
// change algorithm from those originally selected
kdbxHeader.setCipherAlgorithm(Encryption.Cipher.CHA_CHA_20);
kdbxHeader.setKeyDerivationFunction(Encryption.KeyDerivationFunction.ARGON2);
kdbxHeader.setProtectedStreamAlgorithm(Encryption.ProtectedStreamAlgorithm.CHA_CHA_20);
// save it with format options
kdbxDatabase.save(formatV4, credentials, v4OutputStream);
}
/**
* Load KDBX V3 save as KDBX V4
*/
public void loadKdbx4SaveKdbx3(String resourceName, byte[] password, OutputStream v3OutputStream) throws IOException {
DomDatabaseWrapper database;
// password credentials
KdbxCreds credentials = new KdbxCreds(password);
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceName)) {
// load KdbDatabase
database = DomDatabaseWrapper.load(credentials, inputStream);
}
// create a KDBX (database
D kdbxDatabase = getDatabase();
kdbxDatabase.setName("New Database");
kdbxDatabase.setDescription("Migration of KDBX 4 Database to KDBX 3 Database");
// deep copy from group (not including source group, KDB database has simulated root)
kdbxDatabase.getRootGroup().copy(database.getRootGroup());
// choose a stream format - V4 Kdbx and choose some algorithms
KdbxHeader kdbxHeader = new KdbxHeader(KdbxHeader.KdbxHeaderOpts.V3_AES_SALSA_20);
KdbxStreamFormat formatV3 = new KdbxStreamFormat(kdbxHeader);
// save it with format options
kdbxDatabase.save(formatV3, credentials, v3OutputStream);
}
}