From 219253c5183c7ea3dc0f381d3762ae233d22cc25 Mon Sep 17 00:00:00 2001 From: Dave Date: Sun, 21 Dec 2014 16:35:21 +0200 Subject: [PATCH] Interface additions and global changes to allow config to be modified --- .../{NoConfig.java => NoConfigDefault.java} | 65 ++++++++++++------- src/nodash/core/NoConfigInterface.java | 14 ++++ src/nodash/core/NoCore.java | 28 +++++--- src/nodash/core/NoUtil.java | 6 +- 4 files changed, 77 insertions(+), 36 deletions(-) rename src/nodash/core/{NoConfig.java => NoConfigDefault.java} (61%) create mode 100644 src/nodash/core/NoConfigInterface.java diff --git a/src/nodash/core/NoConfig.java b/src/nodash/core/NoConfigDefault.java similarity index 61% rename from src/nodash/core/NoConfig.java rename to src/nodash/core/NoConfigDefault.java index 3fb18cb..cd5ae28 100644 --- a/src/nodash/core/NoConfig.java +++ b/src/nodash/core/NoConfigDefault.java @@ -35,19 +35,19 @@ import javax.crypto.SecretKey; import nodash.exceptions.NoDashFatalException; -public class NoConfig implements Serializable { +public class NoConfigDefault implements Serializable, NoConfigInterface { private static final long serialVersionUID = -8498303909736017075L; - public static final String CONFIG_FILENAME = "noconfig.cfg"; + private static final String CONFIG_FILENAME = "noconfig.cfg"; - public SecretKey secretKey; + private SecretKey secretKey; - public boolean saveDatabase = true; - public String databaseFilename = "nodatabase.hash"; - public boolean saveByteSets = false; - public String byteSetFilename = ""; + private boolean saveDatabase = true; + private String databaseFileName = "nodatabase.hash"; + private boolean saveByteSets = false; - public NoConfig() { + @Override + public void construct() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC); keyGenerator.init(NoUtil.AES_STRENGTH); @@ -56,9 +56,30 @@ public class NoConfig implements Serializable { throw new NoDashFatalException("Value for CIPHER_KEY_SPEC not valid."); } } + + @Override + public SecretKey getSecretKey() { + return secretKey; + } + + @Override + public boolean saveDatabase() { + return saveDatabase; + } - public void saveNoConfigToFile(File file) { + public String getDatabaseName() { + return databaseFileName; + } + + @Override + public boolean saveByteSets() { + return saveByteSets; + } + + @Override + public void saveNoConfig() { try { + File file = new File(NoConfigDefault.CONFIG_FILENAME); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(this); @@ -69,21 +90,19 @@ public class NoConfig implements Serializable { throw new NoDashFatalException("Unable to save config, including generated secret key."); } } - - public static NoConfig getNoConfigFromFile(File file) { + + @Override + public NoConfigInterface loadNoConfig() throws IOException { + File file = new File(NoConfigDefault.CONFIG_FILENAME); + byte[] data = Files.readAllBytes(file.toPath()); + ByteArrayInputStream bais = new ByteArrayInputStream(data); + ObjectInputStream ois = new ObjectInputStream(bais); + NoConfigInterface noConfig; try { - byte[] data = Files.readAllBytes(file.toPath()); - ByteArrayInputStream bais = new ByteArrayInputStream(data); - ObjectInputStream ois = new ObjectInputStream(bais); - NoConfig noConfig; - try { - noConfig = (NoConfig) ois.readObject(); - } catch (ClassNotFoundException e) { - throw new NoDashFatalException("Given bytestream does not compile into a configuration object."); - } - return noConfig; - } catch (IOException e) { - throw new NoDashFatalException("Instructed to read config from file but unable to do so."); + noConfig = (NoConfigDefault) ois.readObject(); + } catch (ClassNotFoundException e) { + throw new NoDashFatalException("Given bytestream does not compile into a configuration object."); } + return noConfig; } } diff --git a/src/nodash/core/NoConfigInterface.java b/src/nodash/core/NoConfigInterface.java new file mode 100644 index 0000000..82f910b --- /dev/null +++ b/src/nodash/core/NoConfigInterface.java @@ -0,0 +1,14 @@ +package nodash.core; + +import java.io.IOException; + +import javax.crypto.SecretKey; + +public interface NoConfigInterface { + public void construct(); + public SecretKey getSecretKey(); + public boolean saveDatabase(); + public boolean saveByteSets(); + public void saveNoConfig(); + public NoConfigInterface loadNoConfig() throws IOException; +} diff --git a/src/nodash/core/NoCore.java b/src/nodash/core/NoCore.java index eb5384c..038e754 100644 --- a/src/nodash/core/NoCore.java +++ b/src/nodash/core/NoCore.java @@ -19,7 +19,7 @@ package nodash.core; -import java.io.File; +import java.io.IOException; import java.security.PublicKey; import nodash.core.spheres.NoByteSetSphere; @@ -39,23 +39,31 @@ import nodash.models.NoUser; import nodash.models.NoSession.NoState; public final class NoCore { - public static NoConfig config; + public static NoConfigInterface config; public static NoHashSphereInterface hashSphere; + public static void setup(NoConfigInterface config, NoHashSphereInterface hashSphere) { + NoCore.setup(config); + NoCore.setup(hashSphere); + } + + public static void setup(NoConfigInterface config) { + NoCore.config = config; + } + public static void setup(NoHashSphereInterface hashSphere) { - File configFile = new File(NoConfig.CONFIG_FILENAME); - if (configFile.exists()) { - config = NoConfig.getNoConfigFromFile(configFile); - } else { - config = new NoConfig(); - config.saveNoConfigToFile(configFile); - } NoCore.hashSphere = hashSphere; hashSphere.setup(); } public static void setup() { - NoCore.setup(new NoHashSphereDefault()); + NoConfigInterface newConfig = new NoConfigDefault(); + try { + newConfig = newConfig.loadNoConfig(); + } catch (IOException e) { + newConfig.construct(); + } + NoCore.setup(newConfig, new NoHashSphereDefault()); } public static byte[] login(byte[] data, char[] password) throws NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionExpiredException { diff --git a/src/nodash/core/NoUtil.java b/src/nodash/core/NoUtil.java index e1b3d0f..996d3cf 100644 --- a/src/nodash/core/NoUtil.java +++ b/src/nodash/core/NoUtil.java @@ -86,7 +86,7 @@ public final class NoUtil { } catch (NoSuchAlgorithmException e) { throw new NoDashFatalException("Value for PBE_TYPE is not valid."); } - KeySpec spec = new PBEKeySpec(password, NoCore.config.secretKey.getEncoded(), 65536, 256); + KeySpec spec = new PBEKeySpec(password, NoCore.config.getSecretKey().getEncoded(), 65536, 256); SecretKey key; try { key = skf.generateSecret(spec); @@ -147,7 +147,7 @@ public final class NoUtil { } public static byte[] encrypt(byte[] data) { - return NoUtil.encrypt(data, NoCore.config.secretKey.getEncoded()); + return NoUtil.encrypt(data, NoCore.config.getSecretKey().getEncoded()); } public static byte[] decrypt(byte[] data, byte[] key) throws IllegalBlockSizeException, BadPaddingException { @@ -170,7 +170,7 @@ public final class NoUtil { } public static byte[] decrypt(byte[] data) throws IllegalBlockSizeException, BadPaddingException { - return NoUtil.decrypt(data, NoCore.config.secretKey.getEncoded()); + return NoUtil.decrypt(data, NoCore.config.getSecretKey().getEncoded()); } public static byte[] encryptRSA(byte[] data, PublicKey publicKey) {