Minor cleanup and refactoring.

This commit is contained in:
Marko Zivanovic
2014-11-23 22:58:51 +01:00
parent 696fa0cf67
commit b30b8d7dcf
5 changed files with 99 additions and 71 deletions
+1
View File
@@ -10,3 +10,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
/target/
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (c) 2014, Marko Živanović Copyright (c) 2014, Marko Zivanovic
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
+31 -15
View File
@@ -5,6 +5,34 @@
<artifactId>share-a-secret</artifactId> <artifactId>share-a-secret</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>share-a-secret</name>
<licenses>
<license>
<name>BSD 2-Clause</name>
<url>http://opensource.org/licenses/BSD-2-Clause</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>marko</id>
<name>Marko Zivanovic</name>
<email>marko@zivanovic.in.rs</email>
<url>http://marko.zivanovic.in.rs</url>
<roles>
<role>developer</role>
</roles>
<timezone>+1</timezone>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@@ -13,11 +41,7 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
@@ -40,14 +64,6 @@
<libs> <libs>
<lib>${java.home}/lib/rt.jar</lib> <lib>${java.home}/lib/rt.jar</lib>
</libs> </libs>
<assembly>
<inclusions>
<!-- <inclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</inclusion>-->
</inclusions>
</assembly>
</configuration> </configuration>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -59,8 +75,8 @@
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>2.5</version> <version>2.5</version>
<configuration> <configuration>
<archive> <archive>
<manifest> <manifest>
@@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* Entry point into application.
* *
* @author marko * @author marko
*/ */
@@ -39,61 +40,71 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
if (args.length == 1 && args[0].toLowerCase().equals("-d")) { if (args.length == 1 && args[0].toLowerCase().equals("-d")) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { decodeFromStdin();
List<SecretShare> shares = new ArrayList<>();
System.out.println("Enter a number of shares, one per line, that you'd like to try to join.");
System.out.println("Finish with empty line.");
while (true) {
String line = br.readLine();
if (line != null && !line.isEmpty()) {
try {
int s = line.indexOf("U1");
if (s >= 0) {
SecretShare share = Utils.decodeFromBinary(Base64.decode(line.substring(s)));
shares.add(share);
} else {
System.err.println("Invalid data");
}
} catch (IllegalArgumentException ex) {
System.err.println(ex.getMessage());
}
} else {
break;
}
}
String secret = ShamirSecretSharing.joinToUtf8String(shares);
System.out.println("Secret = " + secret);
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
} else if (args.length == 3 && args[0].toLowerCase().equals("-e")) { } else if (args.length == 3 && args[0].toLowerCase().equals("-e")) {
Integer total = Integer.valueOf(args[1]); encodeToStdout(Integer.valueOf(args[1]), Integer.valueOf(args[2]));
Integer threshold = Integer.valueOf(args[2]);
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String line = br.readLine();
List<SecretShare> shares = ShamirSecretSharing.split(line, total, threshold);
for (SecretShare share : shares) {
System.out.println(Base64.encodeBytes(Utils.encodeToBinary(share)));
System.out.println();
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
} else { } else {
System.err.println("Share-A-Secret 1.0.0-SNAPSHOT"); showHelp();
System.err.println("This application uses Shamir's secret sharing algorithm to split the secret data into");
System.err.println("a desired number of shares. In order to retrieve the secret data, at least a required");
System.err.println("minimum number of shares must be present.");
System.err.println();
System.err.println("All data is read from standard input and sent to standard output.");
System.err.println();
System.err.println("To encode: java -jar sas.jar -e <n> <t>");
System.err.println("To decode: java -jar sas.jar -d");
System.err.println();
System.err.println(" n - total number of shares to generate");
System.err.println(" t - minimum number of shares required to retrieve the secret.");
System.err.println();
} }
} }
private static void decodeFromStdin() {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
List<SecretShare> shares = new ArrayList<>();
System.out.println("Enter a number of shares, one per line, that you'd like to try to join.");
System.out.println("Finish with empty line.");
while (true) {
String line = br.readLine();
if (line != null && !line.isEmpty()) {
try {
int s = line.indexOf("U1");
if (s >= 0) {
SecretShare share = Utils.decodeFromBinary(Base64.decode(line.substring(s)));
shares.add(share);
} else {
System.err.println("Invalid data");
}
} catch (IllegalArgumentException ex) {
System.err.println(ex.getMessage());
}
} else {
break;
}
}
String secret = ShamirSecretSharing.joinToUtf8String(shares);
System.out.println("Secret = " + secret);
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
private static void encodeToStdout(int totalShares, int thresholdShares) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String line = br.readLine();
List<SecretShare> shares = ShamirSecretSharing.split(line, totalShares, thresholdShares);
for (SecretShare share : shares) {
System.out.println(Base64.encodeBytes(Utils.encodeToBinary(share)));
System.out.println();
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
private static void showHelp() {
System.err.println("Share-A-Secret 1.0.0-SNAPSHOT");
System.err.println("This application uses Shamir's secret sharing algorithm to split the secret data into");
System.err.println("a desired number of shares. In order to retrieve the secret data, at least a required");
System.err.println("minimum number of shares must be present.");
System.err.println();
System.err.println("All data is read from standard input and sent to standard output.");
System.err.println();
System.err.println("To encode: java -jar sas.jar -e <n> <t>");
System.err.println("To decode: java -jar sas.jar -d");
System.err.println();
System.err.println(" n - total number of shares to generate");
System.err.println(" t - minimum number of shares required to retrieve the secret.");
System.err.println();
}
} }
@@ -70,10 +70,10 @@ public final class Utils {
return secret.nextProbablePrime(); return secret.nextProbablePrime();
} }
public static BigInteger getRandomPrimeGreaterThan(BigInteger secret) { public static BigInteger getRandomPrimeGreaterThan(BigInteger prime) {
BigInteger res = BigInteger.ZERO; BigInteger res = BigInteger.ZERO;
while (res.compareTo(secret) <= 0) { while (res.compareTo(prime) <= 0) {
res = BigInteger.probablePrime(secret.bitLength(), RANDOM); res = BigInteger.probablePrime(prime.bitLength(), RANDOM);
} }
return res; return res;
} }