Skip to content

Commit

Permalink
Update jcommander (Bug fix: see cbeust/jcommander#380); adding simple…
Browse files Browse the repository at this point in the history
… parsing
  • Loading branch information
th-tran committed Oct 28, 2018
1 parent 540dbbd commit 6ebe25f
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 49 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To build the project, there are two options:
2) Manual: Run the following commands:
1) javac -cp lib/* -d classes src/\*.java
2) cd classes
3) jar cvfe Cipher.jar Cipher \*.class algorithm/\*.class ../lib/\*.jar
3) jar cvfe Main.jar Main \*.class algorithm/\*.class ../lib/\*.jar

## Running the program

Expand All @@ -18,16 +18,16 @@ There are two options:

1) Class file:
1) cd classes
2) java -cp ".;../lib/\*" Cipher
2) java -cp ".;../lib/\*" Main

2) Jar: java -jar jar/Cipher.jar
2) Jar: java -jar jar/Main.jar

## Current features:
- New project ;)

## Planned features:
- Encryption algorithms:
- Caesar (https://simple.wikipedia.org/wiki/Caesar_cipher)
- Shift (https://simple.wikipedia.org/wiki/Caesar_cipher)
- RSA (https://simple.wikipedia.org/wiki/RSA_algorithm)
- AES (https://en.wikipedia.org/wiki/Advanced_Encryption_Standard)
- Optional modes:
Expand Down
17 changes: 13 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
# Exit script on any error
set -e

if [[ "$1" == "-c" ]] || [[ "$1" == "--clean" ]]; then
echo -e "\nRemoving current class files..."
rm -rf classes/*
echo -e "...done\n"
echo -e "\nRemoving current jar files..."
rm -rf jar/*
echo -e "....done"
fi

echo -e "\nCompiling class files..."
mkdir -p classes
javac -cp lib/* -d classes $(find -name "*.java")
echo -e "...done.\n"
echo -e "Building jar...\n"
mkdir -p classes
cd classes
jar cvfm Cipher.jar ../manifest.mf *.class algorithm/*.class ../lib/*.jar
jar cvfm Main.jar ../manifest.mf *.class cryptography/*.class parser/*.class ../lib/*.jar
mkdir -p ../jar
mv Cipher.jar ../jar
mv Main.jar ../jar
echo -e "...done.\n"
echo "Build complete! Type 'java -jar jar/Cipher.jar' to run the program."
echo "Build complete! Type 'java -jar jar/Main.jar' to run the program."
Empty file removed classes/.keep
Empty file.
Binary file removed classes/Args.class
Binary file not shown.
Binary file removed classes/Cipher.class
Binary file not shown.
Binary file removed classes/algorithm/Caesar.class
Binary file not shown.
Binary file removed classes/algorithm/RSA.class
Binary file not shown.
Binary file removed jar/Cipher.jar
Binary file not shown.
Binary file removed lib/jcommander-1.72.jar
Binary file not shown.
Binary file added lib/jcommander-1.74.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions manifest.mf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: Cipher
Class-Path: ../lib/jcommander-1.72.jar
Main-Class: Main
Class-Path: ../lib/jcommander-1.74.jar
10 changes: 0 additions & 10 deletions src/Args.java

This file was deleted.

17 changes: 0 additions & 17 deletions src/Cipher.java

This file was deleted.

34 changes: 34 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import cryptography.*;
import parser.*;

import com.beust.jcommander.JCommander;

public class Main {

public static void main(String[] argv) throws Exception {
Settings settings = new Settings();
for (String arg : argv){
System.out.println(arg);
}
// All invalid/edge cases are caught during parsing
new JCommander(settings, argv);
new Main().run(settings);
}

public void run(Settings settings) throws Exception {
System.out.println(settings.algo);
System.out.println(settings.shift);
switch (settings.algo) {
case RSA:
// TODO
break;
case SHIFT:
Shift cipher = new Shift();
cipher.encrypt();
break;
default:
// Should be handled by parser, but throw exception here anyways
throw new IllegalArgumentException("Invalid algorithm: " + settings.algo);
}
}
}
11 changes: 0 additions & 11 deletions src/algorithm/Caesar.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/algorithm/RSA.java → src/cryptography/RSA.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package algorithm;
package cryptography;

public class RSA {

Expand Down
11 changes: 11 additions & 0 deletions src/cryptography/Shift.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cryptography;

public class Shift {

public Shift() {
}

public void encrypt() {
System.out.println("Hello, world!");
}
}
18 changes: 18 additions & 0 deletions src/parser/AlgoConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package parser;

import com.beust.jcommander.ParameterException;
import com.beust.jcommander.IStringConverter;

public class AlgoConverter implements IStringConverter<AlgoEnum> {

@Override
public AlgoEnum convert(String value) {
AlgoEnum convertedValue = AlgoEnum.fromString(value);

if (convertedValue == null) {
throw new ParameterException("Value " + value + " cannot be converted to AlgoEnum. " +
"Available values are: rsa, shift.");
}
return convertedValue;
}
}
17 changes: 17 additions & 0 deletions src/parser/AlgoEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package parser;

public enum AlgoEnum {

SHIFT,
RSA;

// converter
public static AlgoEnum fromString(String code) {
for (AlgoEnum algo : AlgoEnum.values()) {
if (algo.toString().equalsIgnoreCase(code)) {
return algo;
}
}
return null;
}
}
12 changes: 12 additions & 0 deletions src/parser/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package parser;

import com.beust.jcommander.Parameter;

public class Settings {

@Parameter(description="The algorithm to use to encrypt/decrypt your message", required=true, converter=AlgoConverter.class)
public AlgoEnum algo;
@Parameter(names={"--shift", "-s"}, description="The shift to apply to each character in your message", validateWith=ShiftValidator.class)
public int shift;

}
16 changes: 16 additions & 0 deletions src/parser/ShiftValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package parser;

import com.beust.jcommander.ParameterException;
import com.beust.jcommander.IParameterValidator;

public class ShiftValidator implements IParameterValidator {

@Override
public void validate(String name, String value) throws ParameterException {
int shift = Integer.parseInt(value);

if (shift < 1 || shift > 26) {
throw new ParameterException("Parameter " + name + " should be between 1 and 26");
}
}
}

0 comments on commit 6ebe25f

Please sign in to comment.