Skip to content

Commit

Permalink
Style
Browse files Browse the repository at this point in the history
  • Loading branch information
kocakosm committed Oct 8, 2020
1 parent 135f378 commit 5f92b5d
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 72 deletions.
18 changes: 14 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<groupId>org.kocakosm</groupId>
<artifactId>nash-cipher</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Nash-Cipher</name>
<url>https://github.com/kocakosm/nash-cipher</url>
Expand Down Expand Up @@ -33,15 +32,26 @@

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
3 changes: 1 addition & 2 deletions src/org/kocakosm/nash/IV.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public boolean equals(Object o)
if (!(o instanceof IV)) {
return false;
}
final IV iv = (IV) o;
return Arrays.equals(bits, iv.bits);
return Arrays.equals(bits, ((IV) o).bits);
}
}
2 changes: 1 addition & 1 deletion src/org/kocakosm/nash/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public boolean equals(Object o)
if (!(o instanceof Key)) {
return false;
}
final Key k = (Key) o;
Key k = (Key) o;
return Arrays.equals(redBits, k.redBits)
&& Arrays.equals(blueBits, k.blueBits)
&& Arrays.equals(redPermutations, k.redPermutations)
Expand Down
7 changes: 4 additions & 3 deletions src/org/kocakosm/nash/NashCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kocakosm.nash;

import java.util.Objects;

/**
* Nash's cipher. Instances of this class are not thread-safe.
*
Expand Down Expand Up @@ -45,9 +47,8 @@ public enum Mode
*/
public NashCipher(Key key, IV iv, Mode mode)
{
if (key == null || mode == null) {
throw new NullPointerException();
}
Objects.requireNonNull(key);
Objects.requireNonNull(mode);
this.key = key;
this.mode = mode;
reset(iv);
Expand Down
8 changes: 4 additions & 4 deletions src/org/kocakosm/nash/io/NashCipherInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.kocakosm.nash.IV;
import org.kocakosm.nash.Key;
import org.kocakosm.nash.NashCipher;
import org.kocakosm.nash.NashCipher.Mode;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
* A {@code NashCipherInputStream} is composed of an inner {@link InputStream}
Expand Down Expand Up @@ -45,10 +47,8 @@ public final class NashCipherInputStream extends InputStream
*/
public NashCipherInputStream(Key key, IV iv, InputStream encrypted)
{
if (encrypted == null) {
throw new NullPointerException();
}
this.cipher = new NashCipher(key, iv, NashCipher.Mode.DECRYPTION);
Objects.requireNonNull(encrypted);
this.cipher = new NashCipher(key, iv, Mode.DECRYPTION);
this.encrypted = new BufferedInputStream(encrypted);
}

Expand Down
8 changes: 4 additions & 4 deletions src/org/kocakosm/nash/io/NashCipherOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.kocakosm.nash.IV;
import org.kocakosm.nash.Key;
import org.kocakosm.nash.NashCipher;
import org.kocakosm.nash.NashCipher.Mode;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

/**
* A {@code NashCipherOutputStream} is composed of an inner {@link OutputStream}
Expand Down Expand Up @@ -46,10 +48,8 @@ public final class NashCipherOutputStream extends OutputStream
*/
public NashCipherOutputStream(Key key, IV iv, OutputStream encrypted)
{
if (encrypted == null) {
throw new NullPointerException();
}
this.cipher = new NashCipher(key, iv, NashCipher.Mode.ENCRYPTION);
Objects.requireNonNull(encrypted);
this.cipher = new NashCipher(key, iv, Mode.ENCRYPTION);
this.encrypted = new BufferedOutputStream(encrypted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
Expand All @@ -24,45 +23,30 @@
*
* @author Osman Koçak
*/
final class ObjectCodec
final class Codec
{
static byte[] encode(Serializable object) throws IOException
{
ObjectOutputStream oos = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
oos = new ObjectOutputStream(out);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(object);
oos.flush();
return out.toByteArray();
} finally {
close(oos);
}
return out.toByteArray();
}

static <T extends Serializable> T decode(byte[] object, Class<T> t)
throws IOException
{
ObjectInputStream ois = null;
ByteArrayInputStream in = new ByteArrayInputStream(object);
try {
ois = new ObjectInputStream(in);
try (ObjectInputStream ois = new ObjectInputStream(in)) {
return t.cast(ois.readObject());
} catch (ClassNotFoundException ex) {
throw new IOException(ex);
} finally {
close(ois);
}
}

private static void close(Closeable stream) throws IOException
{
if (stream != null) {
stream.close();
}
}

private ObjectCodec()
private Codec()
{
/* ... */
}
Expand Down
8 changes: 4 additions & 4 deletions test/org/kocakosm/nash/IVTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
public final class IVTest
{
@Test
public void testConstructor()
public void testCreate()
{
IV iv = IV.create(32);
assertEquals(32, iv.getSize());
assertEquals(32, iv.getBits().length);
}

@Test
public void testConstructorWithNegativeSize()
public void testCreateWithNegativeSize()
{
Executable toTest = () -> IV.create(-1);
assertThrows(IllegalArgumentException.class, toTest);
Expand All @@ -50,10 +50,10 @@ public void testEqualsAndHashCode()
}

@Test
public void testSerialization() throws Exception
public void testSerializationDeserializationRoundTrip() throws Exception
{
IV iv = IV.create(32);
IV decoded = ObjectCodec.decode(ObjectCodec.encode(iv), IV.class);
IV decoded = Codec.decode(Codec.encode(iv), IV.class);
assertNotSame(iv, decoded);
assertEquals(iv, decoded);
}
Expand Down
8 changes: 4 additions & 4 deletions test/org/kocakosm/nash/KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public final class KeyTest
{
@Test
public void testConstructor()
public void testCreate()
{
Key k = Key.create(32);
assertEquals(32, k.getSize());
Expand All @@ -35,7 +35,7 @@ public void testConstructor()
}

@Test
public void testConstructorWithNegativeSize()
public void testCreateWithNegativeSize()
{
Executable toTest = () -> Key.create(-1);
assertThrows(IllegalArgumentException.class, toTest);
Expand All @@ -53,10 +53,10 @@ public void testEqualsAndHashCode()
}

@Test
public void testSerialization() throws Exception
public void testSerializationDeserializationRoundTrip() throws Exception
{
Key k = Key.create(32);
Key decoded = ObjectCodec.decode(ObjectCodec.encode(k), Key.class);
Key decoded = Codec.decode(Codec.encode(k), Key.class);
assertNotSame(k, decoded);
assertEquals(k, decoded);
}
Expand Down
21 changes: 10 additions & 11 deletions test/org/kocakosm/nash/NashCipherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ public final class NashCipherTest
private final Key key = Key.create(64);
private final Random prng = new Random();

@Test
public void testCipher()
{
NashCipher enc = new NashCipher(key, iv, Mode.ENCRYPTION);
NashCipher dec = new NashCipher(key, iv, Mode.DECRYPTION);
byte[] data = new byte[prng.nextInt(4096)];
prng.nextBytes(data);

assertArrayEquals(data, dec.process(enc.process(data)));
}

@Test
public void testConstructorWithNullKey()
{
Expand Down Expand Up @@ -87,4 +76,14 @@ public void testProcessWithNegativeLength()
Executable toTest = () -> enc.process(new byte[0], 1, -1);
assertThrows(IndexOutOfBoundsException.class, toTest);
}

@Test
public void testEncryptionDecryptionRoundTrip()
{
NashCipher enc = new NashCipher(key, iv, Mode.ENCRYPTION);
NashCipher dec = new NashCipher(key, iv, Mode.DECRYPTION);
byte[] data = new byte[prng.nextInt(4096)];
prng.nextBytes(data);
assertArrayEquals(data, dec.process(enc.process(data)));
}
}
18 changes: 10 additions & 8 deletions test/org/kocakosm/nash/io/NashCipherInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import org.kocakosm.nash.IV;
import org.kocakosm.nash.Key;
import org.kocakosm.nash.NashCipher;
import org.kocakosm.nash.NashCipher.Mode;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand All @@ -46,7 +48,7 @@ public void testConstructorWithNullStream()
@Test
public void testAvailable() throws Exception
{
byte[] data = "Hello".getBytes("UTF-8");
byte[] data = "Hello".getBytes(StandardCharsets.UTF_8);
InputStream in = new ByteArrayInputStream(data);
InputStream nash = new NashCipherInputStream(key, iv, in);
assertEquals(in.available(), nash.available());
Expand All @@ -72,7 +74,7 @@ public void testMarkSupported() throws Exception
public void testMark() throws Exception
{
InputStream in = mock(InputStream.class);
NashCipherInputStream nash = new NashCipherInputStream(key, iv, in);
InputStream nash = new NashCipherInputStream(key, iv, in);
Executable toTest = () -> nash.mark(100);
assertThrows(UnsupportedOperationException.class, toTest);
}
Expand All @@ -81,7 +83,7 @@ public void testMark() throws Exception
public void testReset() throws Exception
{
InputStream in = mock(InputStream.class);
NashCipherInputStream nash = new NashCipherInputStream(key, iv, in);
InputStream nash = new NashCipherInputStream(key, iv, in);
Executable toTest = () -> nash.reset();
assertThrows(IOException.class, toTest);
}
Expand All @@ -90,36 +92,36 @@ public void testReset() throws Exception
public void testSkip() throws Exception
{
InputStream in = mock(InputStream.class);
NashCipherInputStream nash = new NashCipherInputStream(key, iv, in);
InputStream nash = new NashCipherInputStream(key, iv, in);
Executable toTest = () -> nash.skip(16);
assertThrows(IOException.class, toTest);
}

@Test
public void testRead() throws Exception
{
byte[] data = "Hello".getBytes("UTF-8");
byte[] data = "Hello".getBytes(StandardCharsets.UTF_8);
InputStream in = new ByteArrayInputStream(data);
InputStream nash = new NashCipherInputStream(key, iv, in);
byte[] decrypted = new byte[data.length];
for (int i = 0; i < data.length; i++) {
decrypted[i] = (byte) nash.read();
}
assertEquals(-1, nash.read());
NashCipher cipher = new NashCipher(key, iv, NashCipher.Mode.DECRYPTION);
NashCipher cipher = new NashCipher(key, iv, Mode.DECRYPTION);
assertArrayEquals(cipher.process(data), decrypted);
}

@Test
public void testReadArray() throws Exception
{
byte[] data = "Hello".getBytes("UTF-8");
byte[] data = "Hello".getBytes(StandardCharsets.UTF_8);
InputStream in = new ByteArrayInputStream(data);
InputStream nash = new NashCipherInputStream(key, iv, in);
byte[] decrypted = new byte[data.length];
nash.read(decrypted);
assertEquals(-1, nash.read(new byte[data.length]));
NashCipher cipher = new NashCipher(key, iv, NashCipher.Mode.DECRYPTION);
NashCipher cipher = new NashCipher(key, iv, Mode.DECRYPTION);
assertArrayEquals(cipher.process(data), decrypted);
}
}
Loading

0 comments on commit 5f92b5d

Please sign in to comment.