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

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;
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 {

View File

@@ -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) {