mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-16 06:53:01 +00:00
Make the slot lookup code a little nicer
This commit is contained in:
parent
e3f4503967
commit
3040276942
2 changed files with 22 additions and 20 deletions
|
@ -44,7 +44,9 @@ import me.impy.aegis.crypto.CryptResult;
|
|||
import me.impy.aegis.crypto.CryptoUtils;
|
||||
import me.impy.aegis.crypto.MasterKey;
|
||||
import me.impy.aegis.crypto.otp.OTP;
|
||||
import me.impy.aegis.crypto.slots.FingerprintSlot;
|
||||
import me.impy.aegis.crypto.slots.PasswordSlot;
|
||||
import me.impy.aegis.crypto.slots.RawSlot;
|
||||
import me.impy.aegis.crypto.slots.Slot;
|
||||
import me.impy.aegis.crypto.slots.SlotCollection;
|
||||
import me.impy.aegis.db.Database;
|
||||
|
@ -364,12 +366,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
private void createDatabase() {
|
||||
database = new Database();
|
||||
try {
|
||||
databaseFile = new DatabaseFile();
|
||||
} catch (Exception e) {
|
||||
// TODO: tell the user to stop using a weird platform
|
||||
throw new UndeclaredThrowableException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
masterKey = new MasterKey(null);
|
||||
|
@ -383,7 +380,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
try {
|
||||
PasswordSlot slot = new PasswordSlot();
|
||||
byte[] salt = CryptoUtils.generateSalt();
|
||||
SecretKey derivedKey = slot.deriveKey("testpassword".toCharArray(), salt, 1000);
|
||||
SecretKey derivedKey = slot.deriveKey("testpassword".toCharArray(), salt, CryptoUtils.CRYPTO_ITERATION_COUNT);
|
||||
Cipher cipher = Slot.createCipher(derivedKey, Cipher.ENCRYPT_MODE);
|
||||
masterKey.encryptSlot(slot, cipher);
|
||||
slots.add(slot);
|
||||
|
@ -407,22 +404,23 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
byte[] content = databaseFile.getContent();
|
||||
if (databaseFile.isEncrypted()) {
|
||||
SlotCollection slots = databaseFile.getSlots();
|
||||
for (Slot slot : slots) {
|
||||
if (slot instanceof PasswordSlot) {
|
||||
try {
|
||||
PasswordSlot derSlot = (PasswordSlot)slot;
|
||||
SecretKey derivedKey = derSlot.deriveKey("testpassword".toCharArray());
|
||||
SlotCollection slots = databaseFile.getSlots();
|
||||
// look up slots in order of preference
|
||||
if (slots.has(FingerprintSlot.class)) {
|
||||
FingerprintSlot slot = slots.find(FingerprintSlot.class);
|
||||
} else if (slots.has(PasswordSlot.class)) {
|
||||
PasswordSlot slot = slots.find(PasswordSlot.class);
|
||||
SecretKey derivedKey = slot.deriveKey("testpassword".toCharArray());
|
||||
Cipher cipher = Slot.createCipher(derivedKey, Cipher.DECRYPT_MODE);
|
||||
masterKey = MasterKey.decryptSlot(slot, cipher);
|
||||
//} else if (slots.has(RawSlot.class)) {
|
||||
} else {
|
||||
throw new Exception("the slot collection doesn't contain any supported slot types");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new UndeclaredThrowableException(e);
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
CryptResult result;
|
||||
try {
|
||||
|
|
|
@ -77,6 +77,10 @@ public class SlotCollection implements Iterable<Slot> {
|
|||
return null;
|
||||
}
|
||||
|
||||
public <T extends Slot> boolean has(Class<T> type) {
|
||||
return find(type) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Slot> iterator() {
|
||||
return _slots.iterator();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue