Interface additions and global changes to allow config to be modified
This commit is contained in:
@@ -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
|
||||||
try {
|
public NoConfigInterface loadNoConfig() throws IOException {
|
||||||
|
File file = new File(NoConfigDefault.CONFIG_FILENAME);
|
||||||
byte[] data = Files.readAllBytes(file.toPath());
|
byte[] data = Files.readAllBytes(file.toPath());
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||||
NoConfig noConfig;
|
NoConfigInterface noConfig;
|
||||||
try {
|
try {
|
||||||
noConfig = (NoConfig) ois.readObject();
|
noConfig = (NoConfigDefault) ois.readObject();
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new NoDashFatalException("Given bytestream does not compile into a configuration object.");
|
throw new NoDashFatalException("Given bytestream does not compile into a configuration object.");
|
||||||
}
|
}
|
||||||
return noConfig;
|
return noConfig;
|
||||||
} catch (IOException e) {
|
|
||||||
throw new NoDashFatalException("Instructed to read config from file but unable to do so.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
14
src/nodash/core/NoConfigInterface.java
Normal file
14
src/nodash/core/NoConfigInterface.java
Normal 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;
|
||||||
|
}
|
||||||
@@ -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(NoHashSphereInterface hashSphere) {
|
public static void setup(NoConfigInterface config, NoHashSphereInterface hashSphere) {
|
||||||
File configFile = new File(NoConfig.CONFIG_FILENAME);
|
NoCore.setup(config);
|
||||||
if (configFile.exists()) {
|
NoCore.setup(hashSphere);
|
||||||
config = NoConfig.getNoConfigFromFile(configFile);
|
|
||||||
} else {
|
|
||||||
config = new NoConfig();
|
|
||||||
config.saveNoConfigToFile(configFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setup(NoConfigInterface config) {
|
||||||
|
NoCore.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setup(NoHashSphereInterface hashSphere) {
|
||||||
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 {
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user