Created extendable NoConfigBase

This commit is contained in:
Dave
2014-12-21 17:17:46 +02:00
parent 219253c518
commit db05ad4866
2 changed files with 53 additions and 25 deletions

View File

@@ -0,0 +1,52 @@
package nodash.core;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import nodash.exceptions.NoDashFatalException;
public abstract class NoConfigBase implements NoConfigInterface {
protected SecretKey secretKey;
protected boolean saveDatabase;
protected boolean saveByteSets;
protected final void generateSecretKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
keyGenerator.init(NoUtil.AES_STRENGTH);
this.secretKey = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC not valid.");
}
}
@Override
public void construct() {
this.generateSecretKey();
}
@Override
public SecretKey getSecretKey() {
return this.secretKey;
}
@Override
public boolean saveDatabase() {
return this.saveDatabase;
}
@Override
public boolean saveByteSets() {
return this.saveByteSets;
}
@Override
public abstract void saveNoConfig();
@Override
public abstract NoConfigInterface loadNoConfig() throws IOException;
}

View File

@@ -28,39 +28,15 @@ import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import nodash.exceptions.NoDashFatalException; import nodash.exceptions.NoDashFatalException;
public class NoConfigDefault implements Serializable, NoConfigInterface { public final class NoConfigDefault extends NoConfigBase implements Serializable {
private static final long serialVersionUID = -8498303909736017075L; private static final long serialVersionUID = -8498303909736017075L;
private static final String CONFIG_FILENAME = "noconfig.cfg"; private static final String CONFIG_FILENAME = "noconfig.cfg";
private SecretKey secretKey;
private boolean saveDatabase = true;
private String databaseFileName = "nodatabase.hash"; private String databaseFileName = "nodatabase.hash";
private boolean saveByteSets = false;
@Override
public void construct() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
keyGenerator.init(NoUtil.AES_STRENGTH);
this.secretKey = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC not valid.");
}
}
@Override
public SecretKey getSecretKey() {
return secretKey;
}
@Override @Override
public boolean saveDatabase() { public boolean saveDatabase() {