cleanup
This commit is contained in:
@@ -7,13 +7,8 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import nodash.core.NoAdapter;
|
||||
import nodash.core.NoUtil;
|
||||
import nodash.exceptions.NoByteSetBadDecryptionException;
|
||||
@@ -58,20 +53,14 @@ public final class NoSession implements Serializable {
|
||||
public NoSession(byte[] data, char[] password) throws NoUserNotValidException {
|
||||
this();
|
||||
this.state = NoState.IDLE;
|
||||
try {
|
||||
byte[] originalData = Arrays.copyOf(data, data.length);
|
||||
char[] originalPassword = Arrays.copyOf(password, password.length);
|
||||
this.original =
|
||||
NoUser.createUserFromFile(originalData, originalPassword, NoUtil.NO_USER_CLASS);
|
||||
this.current = NoUser.createUserFromFile(data, password, NoUtil.NO_USER_CLASS);
|
||||
NoUtil.wipeBytes(data);
|
||||
NoUtil.wipeChars(password);
|
||||
this.uuid = UUID.randomUUID().toString();
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
throw new NoUserNotValidException();
|
||||
} catch (BadPaddingException e) {
|
||||
throw new NoUserNotValidException();
|
||||
}
|
||||
byte[] originalData = Arrays.copyOf(data, data.length);
|
||||
char[] originalPassword = Arrays.copyOf(password, password.length);
|
||||
this.original =
|
||||
NoUser.createUserFromFile(originalData, originalPassword, NoUtil.NO_USER_CLASS);
|
||||
this.current = NoUser.createUserFromFile(data, password, NoUtil.NO_USER_CLASS);
|
||||
NoUtil.wipeBytes(data);
|
||||
NoUtil.wipeChars(password);
|
||||
this.uuid = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public void check() throws NoSessionConfirmedException, NoSessionExpiredException {
|
||||
@@ -118,14 +107,7 @@ public final class NoSession implements Serializable {
|
||||
throw new NoSessionNotAwaitingConfirmationException();
|
||||
}
|
||||
|
||||
NoUser confirmed;
|
||||
try {
|
||||
confirmed = NoUser.createUserFromFile(confirmData, password, NoUtil.NO_USER_CLASS);
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
throw new NoUserNotValidException();
|
||||
} catch (BadPaddingException e) {
|
||||
throw new NoUserNotValidException();
|
||||
}
|
||||
NoUser confirmed = NoUser.createUserFromFile(confirmData, password, NoUtil.NO_USER_CLASS);
|
||||
|
||||
NoUtil.wipeBytes(confirmData);
|
||||
NoUtil.wipeChars(password);
|
||||
|
||||
@@ -23,15 +23,12 @@ import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
@@ -47,12 +44,14 @@ import javax.crypto.spec.SecretKeySpec;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
import sun.security.rsa.RSAPrivateCrtKeyImpl;
|
||||
import sun.security.rsa.RSAPublicKeyImpl;
|
||||
import nodash.core.NoUtil;
|
||||
import nodash.exceptions.NoByteSetBadDecryptionException;
|
||||
import nodash.exceptions.NoDashFatalException;
|
||||
import nodash.exceptions.NoUserNotValidException;
|
||||
|
||||
public class NoUser implements Serializable {
|
||||
private static final long serialVersionUID = 7132405837081692211L;
|
||||
@@ -123,6 +122,7 @@ public class NoUser implements Serializable {
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public final byte[] createHash() {
|
||||
try {
|
||||
List<Object> items = new ArrayList<Object>();
|
||||
@@ -226,14 +226,22 @@ public class NoUser implements Serializable {
|
||||
}
|
||||
|
||||
public static NoUser createUserFromFile(byte[] data, char[] password, Class<? extends NoUser> clazz)
|
||||
throws IllegalBlockSizeException, BadPaddingException {
|
||||
byte[] decrypted = NoUtil.decrypt(data, password);
|
||||
throws NoUserNotValidException {
|
||||
byte[] decrypted;
|
||||
try {
|
||||
decrypted = NoUtil.decrypt(data, password);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
||||
throw new NoUserNotValidException(e);
|
||||
}
|
||||
|
||||
Gson gson = new Gson();
|
||||
String json = NoUtil.fromBytes(decrypted);
|
||||
NoUser noUser = gson.fromJson(json, clazz);
|
||||
|
||||
return noUser;
|
||||
try {
|
||||
NoUser noUser = gson.fromJson(json, clazz);
|
||||
return noUser;
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new NoUserNotValidException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String createHashString() {
|
||||
|
||||
@@ -26,6 +26,7 @@ import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
|
||||
import nodash.core.NoUtil;
|
||||
import nodash.exceptions.NoUserNotValidException;
|
||||
import nodash.models.NoUser;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
@@ -82,7 +83,8 @@ public class NoUserTest {
|
||||
|
||||
@Test
|
||||
public void testCreateUserFromFile()
|
||||
throws IllegalBlockSizeException, BadPaddingException, ClassNotFoundException, IOException {
|
||||
throws IllegalBlockSizeException, BadPaddingException,
|
||||
ClassNotFoundException, IOException, NoUserNotValidException {
|
||||
NoUser user = new NoUser();
|
||||
final byte[] originalFile = user.createFile("password".toCharArray());
|
||||
byte[] file = Arrays.copyOf(originalFile, originalFile.length);
|
||||
@@ -93,7 +95,7 @@ public class NoUserTest {
|
||||
try {
|
||||
user = NoUser.createUserFromFile(file, "wrongpassword".toCharArray(), NoUtil.NO_USER_CLASS);
|
||||
fail("Should have thrown an error when given wrong password.");
|
||||
} catch (BadPaddingException e) {
|
||||
} catch (NoUserNotValidException e) {
|
||||
// Do nothing, correct
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user