import java.io.Reader;
import java.io.Writer;
import java.security.AccessController;
+import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Iterator;
* Setup the keyring persistently, {@link #isSetup()} must return true
* afterwards
*/
- protected abstract void setup();
+ protected abstract void setup(char[] password);
/** Populates the key spec callback */
protected abstract void handleKeySpecCallback(PBEKeySpecCallback pbeCallback);
try {
writer = new OutputStreamWriter(out, charset);
writer.write(arr);
+ writer.flush();
in = new ByteArrayInputStream(out.toByteArray());
set(path, in);
} catch (IOException e) {
this.charset = charset;
}
+ protected static byte[] hash(char[] password, byte[] salt,
+ Integer iterationCount) {
+ ByteArrayOutputStream out = null;
+ OutputStreamWriter writer = null;
+ try {
+ out = new ByteArrayOutputStream();
+ writer = new OutputStreamWriter(out, "UTF-8");
+ writer.write(password);
+ MessageDigest pwDigest = MessageDigest.getInstance("SHA-256");
+ pwDigest.reset();
+ pwDigest.update(salt);
+ byte[] btPass = pwDigest.digest(out.toByteArray());
+ for (int i = 0; i < iterationCount; i++) {
+ pwDigest.reset();
+ btPass = pwDigest.digest(btPass);
+ }
+ return btPass;
+ } catch (Exception e) {
+ throw new ArgeoException("Cannot hash", e);
+ } finally {
+ StreamUtils.closeQuietly(out);
+ StreamUtils.closeQuietly(writer);
+ }
+
+ }
+
class KeyringCallbackHandler implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
defaultCallbackHandler.handle(dialogCbs);
}
- if (passwordCb.getPassword() != null)// not cancelled
- setup();
+ if (passwordCb.getPassword() != null) {// not cancelled
+ setup(passwordCb.getPassword());
+ }
}
if (passwordCb.getPassword() != null)