From db05ad486674c9e8661370646dbb4f083d9d485a Mon Sep 17 00:00:00 2001 From: Dave Date: Sun, 21 Dec 2014 17:17:46 +0200 Subject: [PATCH] Created extendable NoConfigBase --- src/nodash/core/NoConfigBase.java | 52 ++++++++++++++++++++++++++++ src/nodash/core/NoConfigDefault.java | 26 +------------- 2 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 src/nodash/core/NoConfigBase.java diff --git a/src/nodash/core/NoConfigBase.java b/src/nodash/core/NoConfigBase.java new file mode 100644 index 0000000..abf6799 --- /dev/null +++ b/src/nodash/core/NoConfigBase.java @@ -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; + +} diff --git a/src/nodash/core/NoConfigDefault.java b/src/nodash/core/NoConfigDefault.java index cd5ae28..01dca54 100644 --- a/src/nodash/core/NoConfigDefault.java +++ b/src/nodash/core/NoConfigDefault.java @@ -28,39 +28,15 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.nio.file.Files; import java.nio.file.StandardOpenOption; -import java.security.NoSuchAlgorithmException; - -import javax.crypto.KeyGenerator; -import javax.crypto.SecretKey; 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 String CONFIG_FILENAME = "noconfig.cfg"; - private SecretKey secretKey; - - private boolean saveDatabase = true; 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 public boolean saveDatabase() {