Minor cleanup and refactoring.
This commit is contained in:
@@ -10,3 +10,4 @@
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
/target/
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2014, Marko Živanović
|
||||
Copyright (c) 2014, Marko Zivanovic
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -5,6 +5,34 @@
|
||||
<artifactId>share-a-secret</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
@@ -13,11 +41,7 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</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>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -40,14 +64,6 @@
|
||||
<libs>
|
||||
<lib>${java.home}/lib/rt.jar</lib>
|
||||
</libs>
|
||||
<assembly>
|
||||
<inclusions>
|
||||
<!-- <inclusion>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</inclusion>-->
|
||||
</inclusions>
|
||||
</assembly>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -59,8 +75,8 @@
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
|
||||
@@ -32,6 +32,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Entry point into application.
|
||||
*
|
||||
* @author marko
|
||||
*/
|
||||
@@ -39,61 +40,71 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 1 && args[0].toLowerCase().equals("-d")) {
|
||||
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());
|
||||
}
|
||||
decodeFromStdin();
|
||||
} else if (args.length == 3 && args[0].toLowerCase().equals("-e")) {
|
||||
Integer total = Integer.valueOf(args[1]);
|
||||
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());
|
||||
}
|
||||
encodeToStdout(Integer.valueOf(args[1]), Integer.valueOf(args[2]));
|
||||
} else {
|
||||
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();
|
||||
showHelp();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public static BigInteger getRandomPrimeGreaterThan(BigInteger secret) {
|
||||
public static BigInteger getRandomPrimeGreaterThan(BigInteger prime) {
|
||||
BigInteger res = BigInteger.ZERO;
|
||||
while (res.compareTo(secret) <= 0) {
|
||||
res = BigInteger.probablePrime(secret.bitLength(), RANDOM);
|
||||
while (res.compareTo(prime) <= 0) {
|
||||
res = BigInteger.probablePrime(prime.bitLength(), RANDOM);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user