diff --git a/.gitignore b/.gitignore index 32858aa..f2d840b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +/target/ \ No newline at end of file diff --git a/LICENSE b/LICENSE index 0b35d8d..36195a6 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/pom.xml b/pom.xml index 00c2d80..e0131f5 100644 --- a/pom.xml +++ b/pom.xml @@ -5,6 +5,34 @@ share-a-secret 1.0.0-SNAPSHOT jar + + share-a-secret + + + BSD 2-Clause + http://opensource.org/licenses/BSD-2-Clause + repo + + + + + marko + Marko Zivanovic + marko@zivanovic.in.rs + http://marko.zivanovic.in.rs + + developer + + +1 + + + + + UTF-8 + 1.7 + 1.7 + + junit @@ -13,11 +41,7 @@ test - - UTF-8 - 1.7 - 1.7 - + @@ -40,14 +64,6 @@ ${java.home}/lib/rt.jar - - - - - @@ -59,8 +75,8 @@ org.apache.maven.plugins - maven-jar-plugin - 2.5 + maven-jar-plugin + 2.5 diff --git a/src/main/java/rs/in/zivanovic/share/a/secret/Main.java b/src/main/java/rs/in/zivanovic/share/a/secret/Main.java index d348b4f..663651f 100644 --- a/src/main/java/rs/in/zivanovic/share/a/secret/Main.java +++ b/src/main/java/rs/in/zivanovic/share/a/secret/Main.java @@ -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 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 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 "); - 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 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 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 "); + 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(); + } + } diff --git a/src/main/java/rs/in/zivanovic/share/a/secret/Utils.java b/src/main/java/rs/in/zivanovic/share/a/secret/Utils.java index 99b8e8c..0b4c4af 100644 --- a/src/main/java/rs/in/zivanovic/share/a/secret/Utils.java +++ b/src/main/java/rs/in/zivanovic/share/a/secret/Utils.java @@ -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; }