formatting

This commit is contained in:
Dave
2015-06-27 21:50:28 +02:00
parent 7bb6c6fdfa
commit 34b9a748f9
36 changed files with 2381 additions and 2360 deletions

View File

@@ -1,20 +1,18 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoAction is an abstract class to allow for the inheritance of actions which
* users can queue before confirmation (user-to-user and user-to-server).
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoAction is an abstract class to allow for the inheritance of actions which users can queue
* before confirmation (user-to-user and user-to-server).
*/
package nodash.models;
@@ -22,8 +20,11 @@ package nodash.models;
import java.io.Serializable;
public abstract class NoAction implements Serializable {
private static final long serialVersionUID = -194752850197321803L;
public abstract void process();
public abstract void execute();
public abstract void purge();
private static final long serialVersionUID = -194752850197321803L;
public abstract void process();
public abstract void execute();
public abstract void purge();
}

View File

@@ -1,31 +1,29 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoByteSet stores an AES key which has been RSA-4096 encrypted and a data
* stream which has been encrypted by this key.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoByteSet stores an AES key which has been RSA-4096 encrypted and a data stream which has been
* encrypted by this key.
*/
package nodash.models;
public final class NoByteSet {
public byte[] key;
public byte[] data;
public NoByteSet(byte[] key, byte[] data) {
this.key = key;
this.data = data;
}
}
public byte[] key;
public byte[] data;
public NoByteSet(byte[] key, byte[] data) {
this.key = key;
this.data = data;
}
}

View File

@@ -1,22 +1,20 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoInfluence is an abstract class allowing for the subclassing of user
* influences, generated by both actions and the server. Upon login, the user
* consumes NoByteSets (generated by .getByteSet()) into the primary NoInfluence,
* which is then applied to the user with .applyTo(NoUser).
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoInfluence is an abstract class allowing for the subclassing of user influences, generated by
* both actions and the server. Upon login, the user consumes NoByteSets (generated by
* .getByteSet()) into the primary NoInfluence, which is then applied to the user with
* .applyTo(NoUser).
*
* Examples include incoming messages, financial changes or charges or updates.
*/
@@ -40,53 +38,54 @@ import javax.crypto.SecretKey;
import nodash.core.NoUtil;
import nodash.exceptions.NoDashFatalException;
public abstract class NoInfluence implements Serializable {
private static final long serialVersionUID = -7509462039664862920L;
public abstract class NoInfluence implements Serializable {
private static final long serialVersionUID = -7509462039664862920L;
public abstract void applyTo(NoUser user);
public final NoByteSet getByteSet(PublicKey publicKey) {
KeyGenerator keyGen;
try {
keyGen = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC is not valid.", e);
}
keyGen.init(NoUtil.AES_STRENGTH);
SecretKey secretKey = keyGen.generateKey();
byte[] key = secretKey.getEncoded();
byte[] encryptedKey = NoUtil.encryptRSA(key, publicKey);
byte[] data = this.getEncrypted(key);
NoUtil.wipeBytes(key);
return new NoByteSet(encryptedKey, data);
}
private final byte[] getEncrypted(byte[] key) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] encrypted = NoUtil.encrypt(baos.toByteArray(), key);
oos.close();
baos.close();
return encrypted;
} catch (IOException e) {
throw new NoDashFatalException("Unable to write NoInfluence object to byte stream.", e);
}
}
public static NoInfluence decrypt(byte[] data, byte[] key) throws IllegalBlockSizeException,
BadPaddingException, ClassNotFoundException {
byte[] decrypted = NoUtil.decrypt(data, key);
ByteArrayInputStream bais = new ByteArrayInputStream(decrypted);
try {
ObjectInputStream ois = new ObjectInputStream(bais);
NoInfluence noInfluence = (NoInfluence) ois.readObject();
ois.close();
bais.close();
return noInfluence;
} catch (IOException e) {
throw new NoDashFatalException("Unable to read out provided data stream.", e);
}
}
public abstract void applyTo(NoUser user);
public final NoByteSet getByteSet(PublicKey publicKey) {
KeyGenerator keyGen;
try {
keyGen = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC is not valid.", e);
}
keyGen.init(NoUtil.AES_STRENGTH);
SecretKey secretKey = keyGen.generateKey();
byte[] key = secretKey.getEncoded();
byte[] encryptedKey = NoUtil.encryptRSA(key, publicKey);
byte[] data = this.getEncrypted(key);
NoUtil.wipeBytes(key);
return new NoByteSet(encryptedKey, data);
}
private final byte[] getEncrypted(byte[] key) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] encrypted = NoUtil.encrypt(baos.toByteArray(), key);
oos.close();
baos.close();
return encrypted;
} catch (IOException e) {
throw new NoDashFatalException("Unable to write NoInfluence object to byte stream.", e);
}
}
public static NoInfluence decrypt(byte[] data, byte[] key) throws IllegalBlockSizeException, BadPaddingException, ClassNotFoundException {
byte[] decrypted = NoUtil.decrypt(data, key);
ByteArrayInputStream bais = new ByteArrayInputStream(decrypted);
try {
ObjectInputStream ois = new ObjectInputStream(bais);
NoInfluence noInfluence = (NoInfluence) ois.readObject();
ois.close();
bais.close();
return noInfluence;
} catch (IOException e) {
throw new NoDashFatalException("Unable to read out provided data stream.", e);
}
}
}

View File

@@ -21,208 +21,212 @@ import nodash.exceptions.NoSessionNotAwaitingConfirmationException;
import nodash.exceptions.NoUserNotValidException;
public final class NoSession implements Serializable {
private static final long serialVersionUID = 1814807373427948931L;
public static final long SESSION_DURATION = 1000 * 60 * 30; //30 minute sessions
public static enum NoState {
IDLE, MODIFIED, AWAITING_CONFIRMATION, CONFIRMED, CLOSED;
};
private NoUser original;
private NoState state;
private final long expiry;
private boolean newUserSession;
public ArrayList<NoByteSet> incoming;
public NoUser current;
public UUID uuid;
public NoSession() {
this.state = NoState.IDLE;
this.expiry = System.currentTimeMillis() + NoSession.SESSION_DURATION;
this.uuid = UUID.randomUUID();
}
public NoSession(NoUser newUser) {
this();
this.state = NoState.MODIFIED;
this.original = null;
this.current = newUser;
this.newUserSession = true;
}
public NoSession(byte[] data, char[] password) throws NoUserNotValidException {
this();
this.newUserSession = false;
this.state = NoState.IDLE;
char[] passwordDupe = password.clone();
try {
this.original = NoUser.createUserFromFile(data, password);
if (NoCore.hashSphere.checkHash(this.original.createHashString())) {
this.current = NoUser.createUserFromFile(data, passwordDupe);
this.uuid = UUID.randomUUID();
NoUtil.wipeBytes(data);
} else {
throw new NoUserNotValidException();
}
} catch (IOException e) {
throw new NoUserNotValidException();
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
} catch (ClassNotFoundException e) {
throw new NoUserNotValidException();
}
}
public void check() throws NoSessionConfirmedException, NoSessionExpiredException {
if (this.state == NoState.CONFIRMED) {
throw new NoSessionConfirmedException();
} else if (this.state == NoState.CLOSED || System.currentTimeMillis() > this.expiry) {
this.state = NoState.CLOSED;
throw new NoSessionExpiredException();
}
}
public NoState touchState() throws NoSessionConfirmedException, NoSessionExpiredException {
this.check();
if (this.newUserSession) {
if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED;
}
} else {
String originalHash = this.original.createHashString();
String currentHash = this.current.createHashString();
if (originalHash.equals(currentHash)) {
this.state = NoState.IDLE;
} else if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED;
}
}
return this.state;
}
public byte[] initiateSaveAttempt(char[] password) throws NoSessionConfirmedException, NoSessionExpiredException {
this.touchState();
this.state = NoState.AWAITING_CONFIRMATION;
byte[] file = this.current.createFile(password);
NoUtil.wipeChars(password);
return file;
}
public void confirmSave(byte[] confirmData, char[] password) throws NoSessionConfirmedException, NoSessionExpiredException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
this.check();
if (this.state != NoState.AWAITING_CONFIRMATION) {
throw new NoSessionNotAwaitingConfirmationException();
}
NoUser confirmed;
try {
confirmed = NoUser.createUserFromFile(confirmData, password);
} catch (IOException e) {
throw new NoUserNotValidException();
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
} catch (ClassNotFoundException e) {
throw new NoUserNotValidException();
}
NoUtil.wipeBytes(confirmData);
NoUtil.wipeChars(password);
if (confirmed.createHashString().equals(this.current.createHashString())) {
this.state = NoState.CONFIRMED;
/* 5.2: confirmed! */
if (!this.newUserSession) {
/* 5.2.1: remove old hash from array */
try {
NoCore.hashSphere.removeHash(this.original.createHashString());
} catch (IOException e) {
throw new NoDashFatalException("Unable to remove hash on confirm.", e);
}
}
/* 5.2.2: add new hash to array */
try {
NoCore.hashSphere.insertHash(this.current.createHashString());
} catch (IOException e) {
throw new NoDashFatalException("Unable to remove hash on confirm.", e);
}
/* 5.2.3: clear influences as they will not need to be re-applied */
ArrayList<NoAction> actions = this.current.getNoActions();
this.incoming = null;
this.original = null;
this.current = null;
/* 5.2.4: execute NoActions */
for (NoAction action : actions) {
/* It is assumed that actions are not long-running tasks
* It is also assumed that actions have the information they need without the user objects */
action.execute();
action.purge();
}
} else {
throw new NoUserNotValidException();
}
}
public NoState getNoState() throws NoSessionConfirmedException, NoSessionExpiredException {
this.touchState();
return this.state;
}
public NoUser getNoUser() throws NoSessionConfirmedException, NoSessionExpiredException {
this.check();
return this.current;
}
public UUID getUUID() {
return this.uuid;
}
public String getUUIDAsString() {
return this.uuid.toString();
}
public byte[] getEncryptedUUID() {
return NoUtil.encrypt(Base64.encodeBase64(this.uuid.toString().getBytes()));
}
public String getEncryptedUUIDAsString() {
return new String(this.getEncryptedUUID());
}
private static final long serialVersionUID = 1814807373427948931L;
public byte[] getOriginalHash() {
if (this.original != null) {
return this.original.createHash();
} else {
return null;
}
}
public static UUID decryptUUID(byte[] data) throws NoDashSessionBadUUIDException {
if (data == null) {
throw new NoDashSessionBadUUIDException();
}
try {
return UUID.fromString(new String(Base64.decodeBase64(NoUtil.decrypt(data))));
} catch (IllegalArgumentException e) {
throw new NoDashSessionBadUUIDException();
}catch (IllegalBlockSizeException e) {
throw new NoDashSessionBadUUIDException();
} catch (BadPaddingException e) {
throw new NoDashSessionBadUUIDException();
}
}
public static final long SESSION_DURATION = 1000 * 60 * 30; // 30 minute sessions
public void consume(NoByteSet byteSet) throws NoByteSetBadDecryptionException {
this.current.consume(byteSet);
}
public void close() {
this.state = NoState.CLOSED;
}
public static enum NoState {
IDLE, MODIFIED, AWAITING_CONFIRMATION, CONFIRMED, CLOSED;
};
private NoUser original;
private NoState state;
private final long expiry;
private boolean newUserSession;
public ArrayList<NoByteSet> incoming;
public NoUser current;
public UUID uuid;
public NoSession() {
this.state = NoState.IDLE;
this.expiry = System.currentTimeMillis() + NoSession.SESSION_DURATION;
this.uuid = UUID.randomUUID();
}
public NoSession(NoUser newUser) {
this();
this.state = NoState.MODIFIED;
this.original = null;
this.current = newUser;
this.newUserSession = true;
}
public NoSession(byte[] data, char[] password) throws NoUserNotValidException {
this();
this.newUserSession = false;
this.state = NoState.IDLE;
char[] passwordDupe = password.clone();
try {
this.original = NoUser.createUserFromFile(data, password);
if (NoCore.hashSphere.checkHash(this.original.createHashString())) {
this.current = NoUser.createUserFromFile(data, passwordDupe);
this.uuid = UUID.randomUUID();
NoUtil.wipeBytes(data);
} else {
throw new NoUserNotValidException();
}
} catch (IOException e) {
throw new NoUserNotValidException();
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
} catch (ClassNotFoundException e) {
throw new NoUserNotValidException();
}
}
public void check() throws NoSessionConfirmedException, NoSessionExpiredException {
if (this.state == NoState.CONFIRMED) {
throw new NoSessionConfirmedException();
} else if (this.state == NoState.CLOSED || System.currentTimeMillis() > this.expiry) {
this.state = NoState.CLOSED;
throw new NoSessionExpiredException();
}
}
public NoState touchState() throws NoSessionConfirmedException, NoSessionExpiredException {
this.check();
if (this.newUserSession) {
if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED;
}
} else {
String originalHash = this.original.createHashString();
String currentHash = this.current.createHashString();
if (originalHash.equals(currentHash)) {
this.state = NoState.IDLE;
} else if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED;
}
}
return this.state;
}
public byte[] initiateSaveAttempt(char[] password) throws NoSessionConfirmedException,
NoSessionExpiredException {
this.touchState();
this.state = NoState.AWAITING_CONFIRMATION;
byte[] file = this.current.createFile(password);
NoUtil.wipeChars(password);
return file;
}
public void confirmSave(byte[] confirmData, char[] password) throws NoSessionConfirmedException,
NoSessionExpiredException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
this.check();
if (this.state != NoState.AWAITING_CONFIRMATION) {
throw new NoSessionNotAwaitingConfirmationException();
}
NoUser confirmed;
try {
confirmed = NoUser.createUserFromFile(confirmData, password);
} catch (IOException e) {
throw new NoUserNotValidException();
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
} catch (ClassNotFoundException e) {
throw new NoUserNotValidException();
}
NoUtil.wipeBytes(confirmData);
NoUtil.wipeChars(password);
if (confirmed.createHashString().equals(this.current.createHashString())) {
this.state = NoState.CONFIRMED;
/* 5.2: confirmed! */
if (!this.newUserSession) {
/* 5.2.1: remove old hash from array */
try {
NoCore.hashSphere.removeHash(this.original.createHashString());
} catch (IOException e) {
throw new NoDashFatalException("Unable to remove hash on confirm.", e);
}
}
/* 5.2.2: add new hash to array */
try {
NoCore.hashSphere.insertHash(this.current.createHashString());
} catch (IOException e) {
throw new NoDashFatalException("Unable to remove hash on confirm.", e);
}
/* 5.2.3: clear influences as they will not need to be re-applied */
ArrayList<NoAction> actions = this.current.getNoActions();
this.incoming = null;
this.original = null;
this.current = null;
/* 5.2.4: execute NoActions */
for (NoAction action : actions) {
/*
* It is assumed that actions are not long-running tasks It is also assumed that actions
* have the information they need without the user objects
*/
action.execute();
action.purge();
}
} else {
throw new NoUserNotValidException();
}
}
public NoState getNoState() throws NoSessionConfirmedException, NoSessionExpiredException {
this.touchState();
return this.state;
}
public NoUser getNoUser() throws NoSessionConfirmedException, NoSessionExpiredException {
this.check();
return this.current;
}
public UUID getUUID() {
return this.uuid;
}
public String getUUIDAsString() {
return this.uuid.toString();
}
public byte[] getEncryptedUUID() {
return NoUtil.encrypt(Base64.encodeBase64(this.uuid.toString().getBytes()));
}
public String getEncryptedUUIDAsString() {
return new String(this.getEncryptedUUID());
}
public byte[] getOriginalHash() {
if (this.original != null) {
return this.original.createHash();
} else {
return null;
}
}
public static UUID decryptUUID(byte[] data) throws NoDashSessionBadUUIDException {
if (data == null) {
throw new NoDashSessionBadUUIDException();
}
try {
return UUID.fromString(new String(Base64.decodeBase64(NoUtil.decrypt(data))));
} catch (IllegalArgumentException e) {
throw new NoDashSessionBadUUIDException();
} catch (IllegalBlockSizeException e) {
throw new NoDashSessionBadUUIDException();
} catch (BadPaddingException e) {
throw new NoDashSessionBadUUIDException();
}
}
public void consume(NoByteSet byteSet) throws NoByteSetBadDecryptionException {
this.current.consume(byteSet);
}
public void close() {
this.state = NoState.CLOSED;
}
}

View File

@@ -1,21 +1,19 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoUser allows the subclassing of custom user objects whilst keeping the
* core requirements of a NoUser: the public and private keys. It also supports
* the serialization, decryption and NoByteSet consumption.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoUser allows the subclassing of custom user objects whilst keeping the core requirements of a
* NoUser: the public and private keys. It also supports the serialization, decryption and NoByteSet
* consumption.
*/
package nodash.models;
@@ -48,148 +46,152 @@ import nodash.exceptions.NoByteSetBadDecryptionException;
import nodash.exceptions.NoDashFatalException;
public class NoUser implements Serializable {
private static final long serialVersionUID = 7132405837081692211L;
private PublicKey publicKey;
private PrivateKey privateKey;
@SuppressWarnings("unused")
private String randomized;
public int influences;
public int actions;
private ArrayList<NoAction> outgoing = new ArrayList<NoAction>();
public NoUser() {
KeyPairGenerator kpg;
try {
kpg = KeyPairGenerator.getInstance(NoUtil.KEYPAIR_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for KEYPAIR_ALGORITHM is not valid.", e);
}
private static final long serialVersionUID = 7132405837081692211L;
private PublicKey publicKey;
private PrivateKey privateKey;
@SuppressWarnings("unused")
private String randomized;
try {
kpg.initialize(NoUtil.RSA_STRENGTH, SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM, NoUtil.SECURERANDOM_PROVIDER));
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for SECURERANDOM_ALGORITHM not valid.", e);
} catch (NoSuchProviderException e) {
throw new NoDashFatalException("Value for SECURERANDOM_PROVIDER not valid.", e);
}
KeyPair keyPair = kpg.generateKeyPair();
this.publicKey = keyPair.getPublic();
this.privateKey = keyPair.getPrivate();
this.influences = 0;
this.actions = 0;
this.touchRandomizer();
}
private void touchRandomizer() {
byte[] randomBytes = new byte[64];
try {
SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM).nextBytes(randomBytes);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for SECURERANDOM_ALGORITHM not valid.", e);
}
this.randomized = new String(randomBytes);
}
public int influences;
public int actions;
public final byte[] createFile(char[] password) {
ArrayList<NoAction> temp = this.outgoing;
try {
this.touchRandomizer();
this.outgoing = new ArrayList<NoAction>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] encrypted = NoUtil.encrypt(baos.toByteArray(), password);
oos.close();
baos.close();
return encrypted;
} catch (IOException e) {
throw new NoDashFatalException("IO Exception encountered while generating encrypted user file byte stream.", e);
} finally {
this.outgoing = temp;
}
}
public final byte[] createHash() {
ArrayList<NoAction> temp = this.outgoing;
try {
this.outgoing = new ArrayList<NoAction>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] userBytes = baos.toByteArray();
return NoUtil.getHashFromByteArray(userBytes);
} catch (IOException e) {
throw new NoDashFatalException("IO Exception encountered while generating user hash.", e);
} finally {
this.outgoing = temp;
}
}
public final String createHashString() {
return new String(this.createHash());
}
private ArrayList<NoAction> outgoing = new ArrayList<NoAction>();
public final void consume(NoByteSet byteSet) throws NoByteSetBadDecryptionException {
try {
SecretKey secretKey = new SecretKeySpec(decryptRSA(byteSet.key), NoUtil.CIPHER_KEY_SPEC);
byte[] key = secretKey.getEncoded();
secretKey = null;
NoInfluence influence = NoInfluence.decrypt(byteSet.data, key);
NoUtil.wipeBytes(key);
influence.applyTo(this);
this.influences++;
} catch (BadPaddingException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (IllegalBlockSizeException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (ClassNotFoundException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (InvalidKeyException e) {
throw new NoByteSetBadDecryptionException(e);
}
}
public final void addAction(NoAction action) {
this.outgoing.add(action);
this.actions++;
}
public NoUser() {
KeyPairGenerator kpg;
try {
kpg = KeyPairGenerator.getInstance(NoUtil.KEYPAIR_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for KEYPAIR_ALGORITHM is not valid.", e);
}
public final ArrayList<NoAction> getNoActions() {
return this.outgoing;
}
public final BigInteger getPublicExponent() {
return ((RSAPublicKeyImpl) publicKey).getPublicExponent();
}
public final BigInteger getModulus() {
return ((RSAPublicKeyImpl) publicKey).getModulus();
}
public final PublicKey getRSAPublicKey() {
try {
return new RSAPublicKeyImpl(this.getModulus(), this.getPublicExponent());
} catch (InvalidKeyException e) {
throw new NoDashFatalException("Invalid key while re-generating a RSAPublicKey.", e);
}
}
private final byte[] decryptRSA(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
return NoUtil.decryptRSA(data, this.privateKey);
}
public static NoUser createUserFromFile(byte[] data, char[] password) throws IllegalBlockSizeException, BadPaddingException, IOException, ClassNotFoundException {
byte[] decrypted = NoUtil.decrypt(data, password);
ByteArrayInputStream bais = new ByteArrayInputStream(decrypted);
ObjectInputStream ois = new ObjectInputStream(bais);
NoUser noUser = (NoUser) ois.readObject();
ois.close();
bais.close();
return noUser;
}
}
try {
kpg.initialize(NoUtil.RSA_STRENGTH,
SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM, NoUtil.SECURERANDOM_PROVIDER));
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for SECURERANDOM_ALGORITHM not valid.", e);
} catch (NoSuchProviderException e) {
throw new NoDashFatalException("Value for SECURERANDOM_PROVIDER not valid.", e);
}
KeyPair keyPair = kpg.generateKeyPair();
this.publicKey = keyPair.getPublic();
this.privateKey = keyPair.getPrivate();
this.influences = 0;
this.actions = 0;
this.touchRandomizer();
}
private void touchRandomizer() {
byte[] randomBytes = new byte[64];
try {
SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM).nextBytes(randomBytes);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for SECURERANDOM_ALGORITHM not valid.", e);
}
this.randomized = new String(randomBytes);
}
public final byte[] createFile(char[] password) {
ArrayList<NoAction> temp = this.outgoing;
try {
this.touchRandomizer();
this.outgoing = new ArrayList<NoAction>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] encrypted = NoUtil.encrypt(baos.toByteArray(), password);
oos.close();
baos.close();
return encrypted;
} catch (IOException e) {
throw new NoDashFatalException(
"IO Exception encountered while generating encrypted user file byte stream.", e);
} finally {
this.outgoing = temp;
}
}
public final byte[] createHash() {
ArrayList<NoAction> temp = this.outgoing;
try {
this.outgoing = new ArrayList<NoAction>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] userBytes = baos.toByteArray();
return NoUtil.getHashFromByteArray(userBytes);
} catch (IOException e) {
throw new NoDashFatalException("IO Exception encountered while generating user hash.", e);
} finally {
this.outgoing = temp;
}
}
public final String createHashString() {
return new String(this.createHash());
}
public final void consume(NoByteSet byteSet) throws NoByteSetBadDecryptionException {
try {
SecretKey secretKey = new SecretKeySpec(decryptRSA(byteSet.key), NoUtil.CIPHER_KEY_SPEC);
byte[] key = secretKey.getEncoded();
secretKey = null;
NoInfluence influence = NoInfluence.decrypt(byteSet.data, key);
NoUtil.wipeBytes(key);
influence.applyTo(this);
this.influences++;
} catch (BadPaddingException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (IllegalBlockSizeException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (ClassNotFoundException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (InvalidKeyException e) {
throw new NoByteSetBadDecryptionException(e);
}
}
public final void addAction(NoAction action) {
this.outgoing.add(action);
this.actions++;
}
public final ArrayList<NoAction> getNoActions() {
return this.outgoing;
}
public final BigInteger getPublicExponent() {
return ((RSAPublicKeyImpl) publicKey).getPublicExponent();
}
public final BigInteger getModulus() {
return ((RSAPublicKeyImpl) publicKey).getModulus();
}
public final PublicKey getRSAPublicKey() {
try {
return new RSAPublicKeyImpl(this.getModulus(), this.getPublicExponent());
} catch (InvalidKeyException e) {
throw new NoDashFatalException("Invalid key while re-generating a RSAPublicKey.", e);
}
}
private final byte[] decryptRSA(byte[] data) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
return NoUtil.decryptRSA(data, this.privateKey);
}
public static NoUser createUserFromFile(byte[] data, char[] password)
throws IllegalBlockSizeException, BadPaddingException, IOException, ClassNotFoundException {
byte[] decrypted = NoUtil.decrypt(data, password);
ByteArrayInputStream bais = new ByteArrayInputStream(decrypted);
ObjectInputStream ois = new ObjectInputStream(bais);
NoUser noUser = (NoUser) ois.readObject();
ois.close();
bais.close();
return noUser;
}
}

View File

@@ -1,25 +1,22 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoErrorableAction is a subclassing of NoTargetedAction for user-server
* interactions that require an error to be returned if a
* NoCannotGetInfluenceException is thrown. The target is not a destination user,
* but is instead treated as the source.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* As with all sourced actions it is advised to avoid their use unless the logic
* demands it, as it establishes a connection between a server action and a user address.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoErrorableAction is a subclassing of NoTargetedAction for user-server interactions that require
* an error to be returned if a NoCannotGetInfluenceException is thrown. The target is not a
* destination user, but is instead treated as the source.
*
* As with all sourced actions it is advised to avoid their use unless the logic demands it, as it
* establishes a connection between a server action and a user address.
*/
package nodash.models.noactiontypes;
@@ -32,27 +29,27 @@ import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoErrorableAction extends NoTargetedAction {
private static final long serialVersionUID = -6077150774349400823L;
private static final long serialVersionUID = -6077150774349400823L;
public NoErrorableAction(PublicKey source) {
// Note that
super(source);
}
public NoErrorableAction(PublicKey source) {
// Note that
super(source);
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
}
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
}
}
}

View File

@@ -1,25 +1,22 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoHandshakeAction is a subclass of the NoSourcedAction with logic to
* generate an applying influence as well as a returned influence, whilst
* also being capable of returned an error influence if the
* NoCannotGetInfluenceException is thrown.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* As with all two-way or sourced actions, their use should be sparing as
* it establishes a connection between two user addresses.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoHandshakeAction is a subclass of the NoSourcedAction with logic to generate an applying
* influence as well as a returned influence, whilst also being capable of returned an error
* influence if the NoCannotGetInfluenceException is thrown.
*
* As with all two-way or sourced actions, their use should be sparing as it establishes a
* connection between two user addresses.
*/
package nodash.models.noactiontypes;
@@ -31,39 +28,39 @@ import nodash.exceptions.NoCannotGetInfluenceException;
import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoHandshakeAction extends NoSourcedAction {
private static final long serialVersionUID = 3195466136587475680L;
public abstract class NoHandshakeAction extends NoSourcedAction {
private static final long serialVersionUID = 3195466136587475680L;
protected abstract NoInfluence generateReturnedInfluence();
public NoHandshakeAction(PublicKey target, PublicKey source) {
super(target, source);
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
NoInfluence result = this.generateReturnedInfluence();
if (result != null) {
NoByteSet byteSet = result.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
}
}
public void purge() {
super.purge();
}
protected abstract NoInfluence generateReturnedInfluence();
public NoHandshakeAction(PublicKey target, PublicKey source) {
super(target, source);
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
NoInfluence result = this.generateReturnedInfluence();
if (result != null) {
NoByteSet byteSet = result.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
}
}
public void purge() {
super.purge();
}
}

View File

@@ -1,24 +1,21 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoSourcedAction is a subclassing of NoTargetedAction which additionally
* has a source, allowing for errors to be returned if a NoCannotGetInfluenceException
* is thrown.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* It is not advised to make a habit of using these for all user-user interactions
* as they provide a clear link between two users.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoSourcedAction is a subclassing of NoTargetedAction which additionally has a source, allowing
* for errors to be returned if a NoCannotGetInfluenceException is thrown.
*
* It is not advised to make a habit of using these for all user-user interactions as they provide a
* clear link between two users.
*/
package nodash.models.noactiontypes;
@@ -30,35 +27,36 @@ import nodash.exceptions.NoCannotGetInfluenceException;
import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoSourcedAction extends NoTargetedAction {
private static final long serialVersionUID = -2996690472537380062L;
protected PublicKey source;
protected abstract NoInfluence generateTargetInfluence() throws NoCannotGetInfluenceException;
public NoSourcedAction(PublicKey target, PublicKey source) {
super(target);
this.source = source;
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
}
}
public void purge() {
super.purge();
this.source = null;
}
public abstract class NoSourcedAction extends NoTargetedAction {
private static final long serialVersionUID = -2996690472537380062L;
protected PublicKey source;
protected abstract NoInfluence generateTargetInfluence() throws NoCannotGetInfluenceException;
public NoSourcedAction(PublicKey target, PublicKey source) {
super(target);
this.source = source;
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
}
}
public void purge() {
super.purge();
this.source = null;
}
}

View File

@@ -1,20 +1,18 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NoTargetedAction is an abstract subclassing of NoAction aimed at providing the
* basis for an action that has a user target in mind.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* NoTargetedAction is an abstract subclassing of NoAction aimed at providing the basis for an
* action that has a user target in mind.
*/
package nodash.models.noactiontypes;
@@ -29,31 +27,32 @@ import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoTargetedAction extends NoAction {
private static final long serialVersionUID = -8893381130155149646L;
protected PublicKey target;
protected abstract NoInfluence generateTargetInfluence() throws NoCannotGetInfluenceException;
public NoTargetedAction(PublicKey target) {
this.target = target;
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
if (e.getResponseInfluence() != null) {
throw new NoDashFatalException("Unsourced action has generated an error with an undeliverable influence.", e);
}
}
}
public void purge() {
this.target = null;
}
private static final long serialVersionUID = -8893381130155149646L;
protected PublicKey target;
protected abstract NoInfluence generateTargetInfluence() throws NoCannotGetInfluenceException;
public NoTargetedAction(PublicKey target) {
this.target = target;
}
public void execute() {
this.process();
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
if (e.getResponseInfluence() != null) {
throw new NoDashFatalException(
"Unsourced action has generated an error with an undeliverable influence.", e);
}
}
}
public void purge() {
this.target = null;
}
}