Refactored project: split library and command line binary into separate module.

This commit is contained in:
Marko Zivanovic
2015-03-10 21:08:52 +01:00
parent ce9e40f1c5
commit 9bcda4c0ed
15 changed files with 400 additions and 39 deletions
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target/appassembler/lib</directory>
<outputDirectory>lib</outputDirectory>
<excludes>
<exclude>maven-metadata-appassembler.xml</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>target/appassembler/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
</fileSets>
</assembly>
@@ -0,0 +1,54 @@
/*
* The MIT License
*
* Copyright 2015 Marko Zivanovic.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package rs.in.zivanovic.obfuscator;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Joiner;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* Implementation of the obfuscate command.
*
* @author Marko Zivanovic <marko@zivanovic.in.rs>
*/
@Parameters(commandDescription = "De-obfuscate sensitive data")
public class DeObfuscateCommand implements Runnable {
@Parameter(names = {"-k", "--key"}, description = "Master key to use for de-obfuscation", required = true)
private String masterKey;
@Parameter(description = "obfuscated string to de-obfuscate", required = true)
private List<String> data;
@Override
public void run() {
JPasswordObfuscator jpo = new JPasswordObfuscator();
String obfuscated = Joiner.on(' ').skipNulls().join(data);
byte[] deObfuscated = jpo.deObfuscate(masterKey.toCharArray(), obfuscated);
System.out.println(new String(deObfuscated, StandardCharsets.UTF_8));
}
}
@@ -0,0 +1,65 @@
/*
* The MIT License
*
* Copyright 2015 Marko Zivanovic.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package rs.in.zivanovic.obfuscator;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* Main entry point.
*
* @author Marko Zivanovic <marko@zivanovic.in.rs>
*/
public class Main {
public static void main(String args[]) {
JCommander jc = new JCommander();
Map<String, Runnable> commands = new HashMap<>();
Map<String, String[]> aliases = new HashMap<>();
commands.put("o", new ObfuscateCommand());
aliases.put("o", new String[]{"ob", "obfuscate"});
commands.put("d", new DeObfuscateCommand());
aliases.put("d", new String[]{"deob", "deobfuscate"});
for (Entry<String, Runnable> e : commands.entrySet()) {
String[] a = aliases.get(e.getKey());
jc.addCommand(e.getKey(), e.getValue(), a);
}
try {
jc.parse(args);
if (commands.keySet().contains(jc.getParsedCommand())) {
Runnable r = commands.get(jc.getParsedCommand());
r.run();
} else {
jc.usage();
}
} catch (RuntimeException ex) {
System.err.println(ex.getMessage());
}
}
}
@@ -0,0 +1,58 @@
/*
* The MIT License
*
* Copyright 2015 Marko Zivanovic.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package rs.in.zivanovic.obfuscator;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Joiner;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* Implementation of the obfuscate command.
*
* @author Marko Zivanovic <marko@zivanovic.in.rs>
*/
@Parameters(commandDescription = "Obfuscate sensitive data")
public class ObfuscateCommand implements Runnable {
@Parameter(names = {"-k", "--key"}, description = "Master key to use for obfuscation", required = true)
private String masterKey;
@Parameter(names = {"-v", "--version"}, description = "Version of the algorithm to use", required = false)
@SuppressWarnings("FieldMayBeFinal")
private int version = 1;
@Parameter(description = "data to obfuscate", required = true)
private List<String> data;
@Override
public void run() {
JPasswordObfuscator jpo = new JPasswordObfuscator();
byte[] dataBytes = Joiner.on(' ').join(data).getBytes(StandardCharsets.UTF_8);
String s = jpo.obfuscate(masterKey.toCharArray(), dataBytes, version);
System.out.println(s);
}
}