Interface additions and global changes to allow config to be modified

This commit is contained in:
Dave
2014-12-21 16:35:21 +02:00
parent 902a00d625
commit 219253c518
4 changed files with 77 additions and 36 deletions

View File

@@ -35,19 +35,19 @@ import javax.crypto.SecretKey;
import nodash.exceptions.NoDashFatalException; import nodash.exceptions.NoDashFatalException;
public class NoConfig implements Serializable { public class NoConfigDefault implements Serializable, NoConfigInterface {
private static final long serialVersionUID = -8498303909736017075L; 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; private boolean saveDatabase = true;
public String databaseFilename = "nodatabase.hash"; private String databaseFileName = "nodatabase.hash";
public boolean saveByteSets = false; private boolean saveByteSets = false;
public String byteSetFilename = "";
public NoConfig() { @Override
public void construct() {
try { try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC); KeyGenerator keyGenerator = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
keyGenerator.init(NoUtil.AES_STRENGTH); keyGenerator.init(NoUtil.AES_STRENGTH);
@@ -57,8 +57,29 @@ public class NoConfig implements Serializable {
} }
} }
public void saveNoConfigToFile(File file) { @Override
public SecretKey getSecretKey() {
return secretKey;
}
@Override
public boolean saveDatabase() {
return saveDatabase;
}
public String getDatabaseName() {
return databaseFileName;
}
@Override
public boolean saveByteSets() {
return saveByteSets;
}
@Override
public void saveNoConfig() {
try { try {
File file = new File(NoConfigDefault.CONFIG_FILENAME);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos); ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this); oos.writeObject(this);
@@ -70,20 +91,18 @@ public class NoConfig implements Serializable {
} }
} }
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 { try {
byte[] data = Files.readAllBytes(file.toPath()); noConfig = (NoConfigDefault) ois.readObject();
ByteArrayInputStream bais = new ByteArrayInputStream(data); } catch (ClassNotFoundException e) {
ObjectInputStream ois = new ObjectInputStream(bais); throw new NoDashFatalException("Given bytestream does not compile into a configuration object.");
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.");
} }
return noConfig;
} }
} }

View File

@@ -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;
}

View File

@@ -19,7 +19,7 @@
package nodash.core; package nodash.core;
import java.io.File; import java.io.IOException;
import java.security.PublicKey; import java.security.PublicKey;
import nodash.core.spheres.NoByteSetSphere; import nodash.core.spheres.NoByteSetSphere;
@@ -39,23 +39,31 @@ import nodash.models.NoUser;
import nodash.models.NoSession.NoState; import nodash.models.NoSession.NoState;
public final class NoCore { public final class NoCore {
public static NoConfig config; public static NoConfigInterface config;
public static NoHashSphereInterface hashSphere; 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) { 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; NoCore.hashSphere = hashSphere;
hashSphere.setup(); hashSphere.setup();
} }
public static void 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 { public static byte[] login(byte[] data, char[] password) throws NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionExpiredException {

View File

@@ -86,7 +86,7 @@ public final class NoUtil {
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for PBE_TYPE is not valid."); 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; SecretKey key;
try { try {
key = skf.generateSecret(spec); key = skf.generateSecret(spec);
@@ -147,7 +147,7 @@ public final class NoUtil {
} }
public static byte[] encrypt(byte[] data) { 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 { 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 { 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) { public static byte[] encryptRSA(byte[] data, PublicKey publicKey) {