Make configurable which style to output
diff --git a/README.md b/README.md
index b1a9c16..edca939 100644
--- a/README.md
+++ b/README.md
@@ -27,6 +27,15 @@
### Generate and print RSA keys
-You can use this library to generate and print RSA keys:
+You can use this library to generate and print RSA keys.
-`java -cp build/libs/lang-0.1.0-BUILD-SNAPSHOT.jar org.apache.fineract.cn.lang.security.RsaKeyPairFactory`
+You can either generate application-<env>.properties file for Spring applications like this:
+
+`java -cp build/libs/lang-0.1.0-BUILD-SNAPSHOT.jar org.apache.fineract.cn.lang.security.RsaKeyPairFactory SPRING > application-dev.properties`
+
+Or you can generate them and import as operation system variables (Unix):
+```
+java -cp build/libs/lang-0.1.0-BUILD-SNAPSHOT.jar org.apache.fineract.cn.lang.security.RsaKeyPairFactory UNIX > env.vars.txt
+source env.vars.txt
+echo $PUBLIC_KEY_TIMESTAMP
+```
diff --git a/src/main/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactory.java b/src/main/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactory.java
index 8b8e742..d76bb2f 100644
--- a/src/main/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactory.java
+++ b/src/main/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactory.java
@@ -41,11 +41,22 @@
public static void main(String[] args) {
KeyPairHolder keyPair = RsaKeyPairFactory.createKeyPair();
- System.out.println("system.publicKey.exponent=" + keyPair.getPublicKeyExp());
- System.out.println("system.publicKey.modulus=" + keyPair.getPublicKeyMod());
- System.out.println("system.publicKey.timestamp=" + keyPair.getTimestamp());
- System.out.println("system.privateKey.modulus=" + keyPair.getPrivateKeyMod());
- System.out.println("system.privateKey.exponent=" + keyPair.getPrivateKeyExp());
+
+ String style = (args != null && args.length > 0) ?args[0] :"";
+ if ("SPRING".equalsIgnoreCase(style)) {
+ System.out.println("system.publicKey.exponent=" + keyPair.getPublicKeyExp());
+ System.out.println("system.publicKey.modulus=" + keyPair.getPublicKeyMod());
+ System.out.println("system.publicKey.timestamp=" + keyPair.getTimestamp());
+ System.out.println("system.privateKey.modulus=" + keyPair.getPrivateKeyMod());
+ System.out.println("system.privateKey.exponent=" + keyPair.getPrivateKeyExp());
+ }
+ else if ("UNIX".equalsIgnoreCase(style)) {
+ System.out.println("PUBLIC_KEY_EXPONENT=" + keyPair.getPublicKeyExp());
+ System.out.println("PUBLIC_KEY_MODULUS=" + keyPair.getPublicKeyMod());
+ System.out.println("PUBLIC_KEY_TIMESTAMP=" + keyPair.getTimestamp());
+ System.out.println("PRIVATE_KEY_MODULUS=" + keyPair.getPrivateKeyMod());
+ System.out.println("PRIVATE_KEY_EXPONENT=" + keyPair.getPrivateKeyExp());
+ }
}
public static KeyPairHolder createKeyPair() {
diff --git a/src/test/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactoryTest.java b/src/test/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactoryTest.java
index d342449..52a60f9 100644
--- a/src/test/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactoryTest.java
+++ b/src/test/java/org/apache/fineract/cn/lang/security/RsaKeyPairFactoryTest.java
@@ -18,13 +18,29 @@
*/
package org.apache.fineract.cn.lang.security;
+import static junit.framework.TestCase.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.junit.After;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
public class RsaKeyPairFactoryTest {
- public RsaKeyPairFactoryTest() {
- super();
+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
+ private final PrintStream originalOut = System.out;
+
+ @Before
+ public void setUpStreams() {
+ System.setOut(new PrintStream(outContent));
+ }
+
+ @After
+ public void restoreStreams() {
+ System.setOut(originalOut);
}
@Test
@@ -38,8 +54,27 @@
}
@Test
- public void shouldHaveMainMethod() throws Exception {
- RsaKeyPairFactory.main(new String[]{});
+ public void shouldPrintSpringStyle() {
+ RsaKeyPairFactory.main(new String[]{"SPRING"});
+
+ String systemOutput = outContent.toString();
+ assertTrue(systemOutput.contains("system.publicKey.exponent="));
+ assertTrue(systemOutput.contains("system.publicKey.modulus="));
+ assertTrue(systemOutput.contains("system.publicKey.timestamp="));
+ assertTrue(systemOutput.contains("system.privateKey.modulus="));
+ assertTrue(systemOutput.contains("system.privateKey.exponent="));
+ }
+
+ @Test
+ public void shouldPrintUnixStyle() {
+ RsaKeyPairFactory.main(new String[]{"UNIX"});
+
+ String systemOutput = outContent.toString();
+ assertTrue(systemOutput.contains("PUBLIC_KEY_EXPONENT="));
+ assertTrue(systemOutput.contains("PUBLIC_KEY_MODULUS="));
+ assertTrue(systemOutput.contains("PUBLIC_KEY_TIMESTAMP="));
+ assertTrue(systemOutput.contains("PRIVATE_KEY_MODULUS="));
+ assertTrue(systemOutput.contains("PRIVATE_KEY_EXPONENT="));
}
}