From c0fdc44d7cb8262325481e3da5d67ca29f963a2b Mon Sep 17 00:00:00 2001 From: Andreas Simon Date: Fri, 29 Jun 2012 17:11:46 +0200 Subject: [PATCH] Added class Main Allows executing jBCrypt from the command line --- .gitignore | 3 ++ pom.xml | 29 ++++++++++++++++- src/main/java/Main.java | 33 +++++++++++++++++++ src/test/java/TestMain.java | 63 +++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 src/main/java/Main.java create mode 100644 src/test/java/TestMain.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f83e8cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target +*.iml diff --git a/pom.xml b/pom.xml index 3adcae9..3d03102 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,34 @@ org.mindrot jbcrypt - 0.3 + 0.3.1 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.2 + + + + Main + + + + + + + jar jbcrypt diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..7978cf5 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,33 @@ +import org.mindrot.*; + +public class Main { + + static MainUtil MAIN_UTIL = new MainUtil(); + + public static void main(String[] args) { + String password; + String salt; + + if (args.length==0) { + System.out.println("Please specify a secret"); + MAIN_UTIL.exit(1); + } else { + password = args[0]; + salt = MAIN_UTIL.genSalt(); + System.out.println(BCrypt.hashpw(password, salt)); + MAIN_UTIL.exit(0); + } + } + +} + + +class MainUtil { + public void exit(int exitCode) { + System.exit(exitCode); + } + + public String genSalt() { + return BCrypt.gensalt(); + } +} diff --git a/src/test/java/TestMain.java b/src/test/java/TestMain.java new file mode 100644 index 0000000..9473dc0 --- /dev/null +++ b/src/test/java/TestMain.java @@ -0,0 +1,63 @@ +import java.io.*; + +import junit.framework.TestCase; +import org.mindrot.*; + + +public class TestMain extends TestCase { + ByteArrayOutputStream stdout = new ByteArrayOutputStream(); + String fixedSalt = "$2a$10$MkkJUCZctqHDtrcWu0gfOe"; + StubbedMainUtil stubbedMainUtil = new StubbedMainUtil(fixedSalt); + + @Override + public void setUp() throws Exception { + Main.MAIN_UTIL = stubbedMainUtil; + PrintStream printStream = new PrintStream(stdout); + System.setOut(printStream); + } + + public void testIfArgEmptyShowInstruction() throws Exception { + Main.main(new String[0]); + assertEquals("Please specify a secret\n", stdout.toString()); + } + + public void testIfArgEmptyExitsWithStatusCode1() throws Exception { + Main.main(new String[0]); + assertEquals(1, stubbedMainUtil.lastExitCode); + } + + public void testIfArgsProvidedReturnHashedSecret() throws Exception { + String secret = "pa$$w0rd"; + String expectedHash = BCrypt.hashpw(secret, fixedSalt) + "\n"; + + Main.main(new String[]{secret}); + String calculatedHash = stdout.toString(); + + assertEquals(expectedHash, calculatedHash); + } + + public void testIfArgsProvidedExitsWithStatusCode0() throws Exception { + Main.main(new String[]{"dummy"}); + assertEquals(0, stubbedMainUtil.lastExitCode); + } + + private class StubbedMainUtil extends MainUtil { + final String fixedSalt; + int lastExitCode = Integer.MAX_VALUE; + + public StubbedMainUtil(String fixedSalt) { + super(); + this.fixedSalt = fixedSalt; + } + + @Override + public void exit(int exitCode) { + lastExitCode = exitCode; + } + + @Override + public String genSalt() { + return fixedSalt; + } + } +}