Skip to content

Commit bd53a93

Browse files
committed
code cleanup
1 parent 0d80939 commit bd53a93

File tree

14 files changed

+70
-65
lines changed

14 files changed

+70
-65
lines changed

enigma-demo/README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
Software implementation of mechanical [Enigma cypher machine](https://en.wikipedia.org/wiki/Enigma_machine).
33

44
```
5-
int[] initialRotorPositions = new int[3];
6-
initialRotorPositions[0] = 18;
7-
initialRotorPositions[1] = 12;
8-
initialRotorPositions[2] = 21;
9-
Enigma enigmaForEncryption = Enigma.builder().createEnigmaBase64(initialRotorPositions).build();
10-
Enigma enigmaForDecryption = Enigma.builder().createEnigmaBase64(initialRotorPositions).build();
5+
Enigma enigmaForEncryption = Enigma.builder()
6+
.createEnigmaAlphabetBase64Rotors3(18, 12, 21)
7+
.build();
8+
Enigma enigmaForDecryption = Enigma.builder()
9+
.createEnigmaAlphabetBase64Rotors3(18, 12, 21)
10+
.build();
1111
1212
String originalMessage = "Hello World !";
13+
1314
String encryptedMessage = enigmaForEncryption.encryptGenericString(originalMessage);
1415
String decryptedMessage = enigmaForDecryption.decryptGenericString(encryptedMessage);
16+
17+
assertEquals(originalMessage, decryptedMessage);
18+
assertNotEquals(originalMessage, encryptedMessage);
19+
assertNotEquals(decryptedMessage, encryptedMessage);
1520
```

enigma-demo/src/main/java/itx/java/examples/enigma/Enigma.java

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public interface Enigma {
4545
*/
4646
void resetPosition();
4747

48+
/**
49+
* Clone this Enigma machine.
50+
* @return
51+
*/
52+
Enigma copy();
53+
4854
/**
4955
* Get enigma builder.
5056
* @return
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,38 @@
11
package itx.java.examples.enigma.configuration;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
36
import java.util.List;
47

58
/**
69
* Created by gergej on 24.1.2017.
710
*/
811
public class EnigmaConfiguration {
912

10-
private String aplhabet;
11-
private List<String> rotorParameters;
12-
private EnigmaSettings enigmaSetup;
13-
14-
public EnigmaConfiguration() {
15-
}
13+
private final String alphabet;
14+
private final List<String> rotorParameters;
15+
private final EnigmaSettings enigmaSetup;
1616

17-
public EnigmaConfiguration(String aplhabet, List<String> rotorParameters, EnigmaSettings enigmaSetup) {
18-
this.aplhabet = aplhabet;
17+
@JsonCreator
18+
public EnigmaConfiguration(@JsonProperty("alphabet") String alphabet,
19+
@JsonProperty("rotorParameters") List<String> rotorParameters,
20+
@JsonProperty("enigmaSetup") EnigmaSettings enigmaSetup) {
21+
this.alphabet = alphabet;
1922
this.rotorParameters = rotorParameters;
2023
this.enigmaSetup = enigmaSetup;
2124
}
2225

23-
public String getAplhabet() {
24-
return aplhabet;
25-
}
26-
27-
public void setAplhabet(String aplhabet) {
28-
this.aplhabet = aplhabet;
26+
public String getAlphabet() {
27+
return alphabet;
2928
}
3029

3130
public List<String> getRotorParameters() {
3231
return rotorParameters;
3332
}
3433

35-
public void setRotorParameters(List<String> rotorParameters) {
36-
this.rotorParameters = rotorParameters;
37-
}
38-
3934
public EnigmaSettings getEnigmaSetup() {
4035
return enigmaSetup;
4136
}
4237

43-
public void setEnigmaSetup(EnigmaSettings enigmaSetup) {
44-
this.enigmaSetup = enigmaSetup;
45-
}
46-
4738
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package itx.java.examples.enigma.configuration;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
36
import java.util.List;
47

58
/**
69
* Created by gergej on 24.1.2017.
710
*/
811
public class EnigmaSettings {
912

10-
private List<Integer> rotorOrdinals;
11-
private List<Character> rotorStartingPositions;
12-
private Character[][] plugBoardSetup;
13-
14-
public EnigmaSettings() {
15-
}
13+
private final List<Integer> rotorOrdinals;
14+
private final List<Character> rotorStartingPositions;
15+
private final Character[][] plugBoardSetup;
1616

17-
public EnigmaSettings(List<Integer> rotorOrdinals,
18-
List<Character> rotorStartingPositions,
19-
Character[][] plugBoardSetup) {
17+
@JsonCreator
18+
public EnigmaSettings(@JsonProperty("rotorOrdinals") List<Integer> rotorOrdinals,
19+
@JsonProperty("rotorStartingPositions") List<Character> rotorStartingPositions,
20+
@JsonProperty("plugBoardSetup") Character[][] plugBoardSetup) {
2021
this.rotorOrdinals = rotorOrdinals;
2122
this.rotorStartingPositions = rotorStartingPositions;
2223
this.plugBoardSetup = plugBoardSetup;
@@ -26,24 +27,12 @@ public List<Integer> getRotorOrdinals() {
2627
return rotorOrdinals;
2728
}
2829

29-
public void setRotorOrdinals(List<Integer> rotorOrdinals) {
30-
this.rotorOrdinals = rotorOrdinals;
31-
}
32-
3330
public List<Character> getRotorStartingPositions() {
3431
return rotorStartingPositions;
3532
}
3633

37-
public void setRotorStartingPositions(List<Character> rotorStartingPositions) {
38-
this.rotorStartingPositions = rotorStartingPositions;
39-
}
40-
4134
public Character[][] getPlugBoardSetup() {
4235
return plugBoardSetup;
4336
}
4437

45-
public void setPlugBoardSetup(Character[][] plugBoardSetup) {
46-
this.plugBoardSetup = plugBoardSetup;
47-
}
48-
4938
}

enigma-demo/src/main/java/itx/java/examples/enigma/impl/enigma/EnigmaBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public EnigmaBuilder setPlugBoard(PlugBoard plugBoard) {
4242

4343
public EnigmaBuilder fromConfiguration(EnigmaConfiguration enigmaConfiguration) {
4444
this.alphabet = Alphabet.builder()
45-
.setAlphabet(enigmaConfiguration.getAplhabet())
45+
.setAlphabet(enigmaConfiguration.getAlphabet())
4646
.build();
4747
this.reflector = Reflector.builder()
4848
.setSubstitutionTable(EnigmaUtils.createReflectorSubstitutionMap(alphabet))

enigma-demo/src/main/java/itx/java/examples/enigma/impl/enigma/EnigmaImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@ public void resetPosition() {
7474
rotorGroup.resetPosition();
7575
}
7676

77+
@Override
78+
public Enigma copy() {
79+
return new EnigmaImpl(alphabet, reflector, rotorGroup, plugBoard);
80+
}
81+
7782
}

enigma-demo/src/main/java/itx/java/examples/enigma/thebomb/TheBomb.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,19 @@ public EnigmaConfiguration findEnigmaSetup() throws ConfigNotFoundException {
4646
}
4747

4848
//TODO: implement iteration through all plugboard setup possibilities
49-
EnigmaSettings enigmaSettings = new EnigmaSettings(null, null, plugBoardSetup);
50-
EnigmaConfiguration result = new EnigmaConfiguration(enigmaConfiguration.getAplhabet(), enigmaConfiguration.getRotorParameters(), enigmaSettings);
5149

5250
for (int i=0; i<rotorGroups.size(); i++) {
53-
//iterate through all rotor group posiibilities
51+
//iterate through all rotor group possibilities
5452
PermutationIterator<Integer> pe = new PermutationIterator<>(rotorGroups.get(i));
5553
while (pe.hasNext()) {
5654
//iterate through all rotor order possibilities
5755
List<Integer> rotorOrdinals = pe.next();
58-
RotorGroupIterator rotorGroupIterator = new RotorGroupIterator(rotorOrdinals.size(), enigmaConfiguration.getAplhabet());
56+
RotorGroupIterator rotorGroupIterator = new RotorGroupIterator(rotorOrdinals.size(), enigmaConfiguration.getAlphabet());
5957
while (rotorGroupIterator.hasNext()) {
60-
//iterate through all rotor starting positions
6158
List<Character> rotorStartingPositions = rotorGroupIterator.getNext();
62-
enigmaSettings.setRotorOrdinals(rotorOrdinals);
63-
enigmaSettings.setRotorStartingPositions(rotorStartingPositions);
59+
EnigmaSettings enigmaSettings = new EnigmaSettings(rotorOrdinals, rotorStartingPositions, plugBoardSetup);
60+
EnigmaConfiguration result = new EnigmaConfiguration(enigmaConfiguration.getAlphabet(), enigmaConfiguration.getRotorParameters(), enigmaSettings);
61+
//iterate through all rotor starting positions
6462
printSetup(rotorOrdinals, rotorStartingPositions);
6563
if (testEnigmaSetup(result, encodedMessage, expectedString)) {
6664
duration = System.currentTimeMillis() - duration;

enigma-demo/src/test/java/itx/java/examples/enigma/tests/BruteForceBreakTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void findEnigmaSetup() throws IOException {
3434
String encryptedMessage = enigmaForEncryption.encryptOrDecrypt(originalMessage);
3535

3636
LOG.info("starting the bomb ...");
37-
EnigmaConfiguration config = new EnigmaConfiguration(enigmaConfiguration.getAplhabet(), enigmaConfiguration.getRotorParameters(), null);
37+
EnigmaConfiguration config = new EnigmaConfiguration(enigmaConfiguration.getAlphabet(), enigmaConfiguration.getRotorParameters(), null);
3838
TheBomb theBomb = new TheBomb(config, encryptedMessage, messagePart, 3, enigmaConfiguration.getEnigmaSetup().getPlugBoardSetup());
3939
EnigmaConfiguration calculatedConfiguration = null;
4040

@@ -54,6 +54,8 @@ public void findEnigmaSetup() throws IOException {
5454
Assert.assertNotNull(encryptedMessage);
5555
Assert.assertNotNull(decryptedMessage);
5656
Assert.assertEquals(originalMessage, decryptedMessage);
57+
Assert.assertNotEquals(originalMessage, encryptedMessage);
58+
Assert.assertNotEquals(decryptedMessage, encryptedMessage);
5759
}
5860

5961
@Test

enigma-demo/src/test/java/itx/java/examples/enigma/tests/EnigmaTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public void testSimpleMessageAlphabet26(String originalMessage) {
4343
Assert.assertEquals(originalMessage.length(), encryptedMessage.length());
4444
Assert.assertNotNull(decryptedMessage);
4545
Assert.assertEquals(encryptedMessage.length(), decryptedMessage.length());
46+
4647
Assert.assertEquals(originalMessage, decryptedMessage);
48+
Assert.assertNotEquals(originalMessage, encryptedMessage);
49+
Assert.assertNotEquals(decryptedMessage, encryptedMessage);
4750
}
4851

4952
@DataProvider(name = "messagesBase64")
@@ -71,7 +74,10 @@ public void testSimpleMessageAlphabetBase64(String inputStreamClassPath) throws
7174
Assert.assertNotNull(decryptedMessage);
7275
String prettyPrint = EnigmaUtils.prettyPrint(12, encryptedMessage);
7376
Assert.assertNotNull(prettyPrint);
77+
7478
Assert.assertEquals(originalMessage, decryptedMessage);
79+
Assert.assertNotEquals(originalMessage, encryptedMessage);
80+
Assert.assertNotEquals(decryptedMessage, encryptedMessage);
7581
}
7682

7783
@DataProvider(name = "configs")
@@ -100,7 +106,10 @@ public void testEnigmaConfiguration(String configPath, String originalMessage, B
100106
}
101107
Assert.assertNotNull(encryptedMessage);
102108
Assert.assertNotNull(decryptedMessage);
109+
103110
Assert.assertEquals(originalMessage, decryptedMessage);
111+
Assert.assertNotEquals(originalMessage, encryptedMessage);
112+
Assert.assertNotEquals(decryptedMessage, encryptedMessage);
104113
}
105114

106115
}

enigma-demo/src/test/java/itx/java/examples/enigma/tests/PartialTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void testRotor() {
7878
for (int i = 0; i < rotor0Data26.length; i++) {
7979
Integer result = rotor.substituteForward(i);
8080
Integer reverse = rotor.substituteReverse(result);
81-
Assert.assertEquals(reverse, new Integer(i));
81+
Assert.assertEquals(reverse, Integer.valueOf(i));
8282
}
8383
}
8484

@@ -92,7 +92,7 @@ public void testShiftedRotor() {
9292
for (int i = 0; i < rotor0Data26.length; i++) {
9393
Integer result = rotor.substituteForward(i);
9494
Integer reverse = rotor.substituteReverse(result);
95-
Assert.assertEquals(reverse, new Integer(i));
95+
Assert.assertEquals(reverse, Integer.valueOf(i));
9696
}
9797
}
9898

enigma-demo/src/test/java/itx/java/examples/enigma/tests/UtilsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
public class UtilsTest {
1616

1717
@Test
18-
public void readEnigmaConfigutationTest() throws IOException {
18+
public void readEnigmaConfigurationTest() throws IOException {
1919
InputStream is = UtilsTest.class.getClassLoader().getResourceAsStream("configurations/enigma-test-configuration-26-01.json");
2020
Assert.assertNotNull(is);
2121
EnigmaConfiguration enigmaConfiguration = EnigmaUtils.readEnigmaConfiguration(is);
2222
Assert.assertNotNull(enigmaConfiguration);
23-
Assert.assertNotNull(enigmaConfiguration.getAplhabet());
23+
Assert.assertNotNull(enigmaConfiguration.getAlphabet());
2424
Assert.assertNotNull(enigmaConfiguration.getRotorParameters());
2525
Assert.assertTrue(enigmaConfiguration.getRotorParameters().size() > 0);
2626
Assert.assertNotNull(enigmaConfiguration.getEnigmaSetup());

enigma-demo/src/test/resources/configurations/enigma-test-configuration-26-01.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22

3-
"aplhabet" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
3+
"alphabet" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
44
"rotorParameters" : [
55
"FYLDVAEQHMCZNXTKJPWUOGISBR",
66
"SXROTNJHUBECYWPDGMAFIZVKQL",

enigma-demo/src/test/resources/configurations/enigma-test-configuration-26-02.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22

3-
"aplhabet" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
3+
"alphabet" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
44
"rotorParameters" : [
55
"FYLDVAEQHMCZNXTKJPWUOGISBR",
66
"SXROTNJHUBECYWPDGMAFIZVKQL",

enigma-demo/src/test/resources/configurations/enigma-test-configuration-base64-01.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22

3-
"aplhabet" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=#",
3+
"alphabet" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=#",
44
"rotorParameters" : [
55
"2AFyaRcKfVvu1=Dp5+iYw9TXbOqm0NQdeJgnEG#hrUx3t8CWMHkzSs7ZPlL46oI/Bj",
66
"9C/c2YUL6#jbnhJGkDuK7SW5PpTqswzoig1OX+0VyrvdmHFQAEI3Rx=BZtN48ealMf",

0 commit comments

Comments
 (0)