Created extendable NoConfigBase
This commit is contained in:
52
src/nodash/core/NoConfigBase.java
Normal file
52
src/nodash/core/NoConfigBase.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user