+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * 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.argeo.util.crypto;
import java.io.ByteArrayInputStream;
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)