-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run key derivation for Authenticator Pro importer on background thread
- Loading branch information
1 parent
58b8edf
commit efd8e2d
Showing
3 changed files
with
123 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/beemdevelopment/aegis/ui/tasks/PBKDFTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.beemdevelopment.aegis.ui.tasks; | ||
|
||
import android.content.Context; | ||
|
||
import com.beemdevelopment.aegis.R; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.KeySpec; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.PBEKeySpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
public class PBKDFTask extends ProgressDialogTask<PBKDFTask.Params, SecretKey> { | ||
private final Callback _cb; | ||
|
||
public PBKDFTask(Context context, Callback cb) { | ||
super(context, context.getString(R.string.unlocking_vault)); | ||
_cb = cb; | ||
} | ||
|
||
@Override | ||
protected SecretKey doInBackground(Params... args) { | ||
setPriority(); | ||
|
||
Params params = args[0]; | ||
return deriveKey(params); | ||
} | ||
|
||
public static SecretKey deriveKey(Params params) { | ||
try { | ||
SecretKeyFactory factory = SecretKeyFactory.getInstance(params.getAlgorithm()); | ||
KeySpec spec = new PBEKeySpec(params.getPassword(), params.getSalt(), params.getIterations(), params.getKeySize()); | ||
SecretKey key = factory.generateSecret(spec); | ||
return new SecretKeySpec(key.getEncoded(), "AES"); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(SecretKey key) { | ||
super.onPostExecute(key); | ||
_cb.onTaskFinished(key); | ||
} | ||
|
||
public interface Callback { | ||
void onTaskFinished(SecretKey key); | ||
} | ||
|
||
public static class Params { | ||
private final String _algorithm; | ||
private final int _keySize; | ||
private final char[] _password; | ||
private final byte[] _salt; | ||
private final int _iterations; | ||
|
||
public Params(String algorithm, int keySize, char[] password, byte[] salt, int iterations) { | ||
_algorithm = algorithm; | ||
_keySize = keySize; | ||
_iterations = iterations; | ||
_password = password; | ||
_salt = salt; | ||
} | ||
|
||
public String getAlgorithm() { | ||
return _algorithm; | ||
} | ||
|
||
public int getKeySize() { | ||
return _keySize; | ||
} | ||
|
||
public char[] getPassword() { | ||
return _password; | ||
} | ||
|
||
public int getIterations() { | ||
return _iterations; | ||
} | ||
|
||
public byte[] getSalt() { | ||
return _salt; | ||
} | ||
} | ||
} |