package net.sourceforge.javautil.common.password.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.javautil.common.ByteUtil;
import net.sourceforge.javautil.common.StringUtil;
import net.sourceforge.javautil.common.encryption.IEncryptionProvider;
import net.sourceforge.javautil.common.encryption.impl.SimpleEncryptionKey;
import net.sourceforge.javautil.common.encryption.impl.SimpleEncryptionProvider;
import net.sourceforge.javautil.common.encryption.impl.SimpleEncryptionKey.Strength;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.IVirtualArtifact;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.password.EncryptedPassword;
import net.sourceforge.javautil.common.password.IPassword;
import net.sourceforge.javautil.common.password.IPasswordLocker;
import net.sourceforge.javautil.common.password.PasswordLockerException;
/**
* This will use an {@link IEncryptionProvider} for encrypting and a
* {@link IVirtualArtifact} for permanent storage.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: StandardPasswordLocker.java 2712 2011-01-03 00:32:01Z ponderator $
*/
public class StandardPasswordLocker implements IPasswordLocker {
private final String name;
private final IEncryptionProvider provider;
private final IEncryptionProvider passwordProvider;
private final IVirtualFile store;
private final Map<String, byte[]> passwords = new HashMap<String, byte[]>();
public StandardPasswordLocker(String name, IEncryptionProvider provider, byte[] password) throws NoSuchAlgorithmException, InvalidKeyException {
this(name, provider, null, password);
}
public StandardPasswordLocker(String name, IEncryptionProvider provider, IVirtualFile store, byte[] password) throws NoSuchAlgorithmException, InvalidKeyException {
this.name = name;
this.store = store;
this.provider = provider;
byte[] lockerPassword = provider.encrypt(password);
this.passwordProvider = new SimpleEncryptionProvider(SimpleEncryptionKey.createUsing(Strength.STRONG, "AES", lockerPassword));
this.load();
}
public IPassword getPassword(String id) throws PasswordLockerException {
return this.passwords.containsKey(id) ? new StandardPassword(id) : null;
}
public void setPassword(String id, IPassword password) throws PasswordLockerException {
this.passwords.put(id, this.passwordProvider.encrypt(password.getPassword()));
}
public List<String> getPasswordIdList() { return new ArrayList<String>(this.passwords.keySet()); }
public void removePassword(String id) { this.passwords.remove(id); }
public String getName() { return name; }
/**
* Load password store from the {@link IVirtualFile} passed in the constructor
*/
public void load () {
if (this.store == null) return;
if (!this.store.isExists()) return;
try {
String contents = this.store.readAsText();
String[] passwords = contents.split(":");
for (String password : passwords) {
String[] parts = password.split(";");
String id = new String( this.passwordProvider.decrypt( ByteUtil.dehexify( parts[0] ) ) );
byte[] pw = ByteUtil.dehexify( parts[1] );
this.passwords.put(id, pw);
}
} catch (Exception e) {
ThrowableManagerRegistry.caught(e);
}
}
/**
* Save the password locker to the {@link IVirtualFile} passed in the constructor.
*
* @throws IOException
*/
public void save () {
if (this.store == null) return;
OutputStream out = null;
try {
out = this.store.getOutputStream();
boolean start = false;
for (String id : this.passwords.keySet()) {
byte[] ide = this.passwordProvider.encrypt(id.getBytes());
byte[] pae = this.passwords.get(id);
if (start) out.write(":".getBytes());
out.write(StringUtil.hexify(ide).getBytes());
out.write(";".getBytes());
out.write(StringUtil.hexify(pae).getBytes());
start = true;
}
} catch (IOException e) {
throw ThrowableManagerRegistry.caught(e);
} finally {
if (out != null) try { out.close(); } catch (IOException e) { ThrowableManagerRegistry.caught(e); }
}
}
public String toString () { return this.name; }
private class StandardPassword implements EncryptedPassword {
protected String id;
public StandardPassword(String id) {
this.id = id;
}
public byte[] getPassword() { return passwordProvider.decrypt(passwords.get(id)); }
}
}