commit 6b32765fe392d94a3ff2e5b59fd9e08426ee5f8b
parent d6ff66553dbb9de941e29fc6c8773a0222013524
Author: Wim Dupont <wim@wimdupont.com>
Date: Sat, 7 Oct 2023 11:20:00 +0200
version upgrade and encapsulation
Diffstat:
5 files changed, 75 insertions(+), 55 deletions(-)
diff --git a/pom.xml b/pom.xml
@@ -17,13 +17,13 @@
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpg-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
- <artifactId>bcpg-jdk15on</artifactId>
- <version>1.70</version>
+ <artifactId>bcpg-jdk18on</artifactId>
+ <version>1.76</version>
</dependency>
<dependency>
<groupId>com.github.bastiaanjansen</groupId>
<artifactId>otp-java</artifactId>
- <version>2.0.1</version>
+ <version>2.0.2</version>
</dependency>
</dependencies>
diff --git a/src/main/java/com/wimdupont/Main.java b/src/main/java/com/wimdupont/Main.java
@@ -2,14 +2,19 @@ package com.wimdupont;
import com.bastiaanjansen.otp.TOTPGenerator;
import com.wimdupont.service.GpgUtil;
+import com.wimdupont.service.PropertiesLoader;
import org.bouncycastle.openpgp.PGPException;
import java.io.IOException;
public class Main {
+
public static void main(String[] args) throws PGPException, IOException {
- String fileArgument = args.length > 0 ? args[0] : null;
- System.out.println(TOTPGenerator.withDefaultValues(new GpgUtil().decrypt(fileArgument)).now());
+ String fileArgument = args.length > 0
+ ? args[0]
+ : null;
+ GpgUtil gpgUtil = new GpgUtil(new PropertiesLoader().loadProperties());
+ System.out.println(TOTPGenerator.withDefaultValues(gpgUtil.decrypt(fileArgument)).now());
System.exit(0);
}
}
diff --git a/src/main/java/com/wimdupont/service/GpgUtil.java b/src/main/java/com/wimdupont/service/GpgUtil.java
@@ -18,12 +18,7 @@ import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder;
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
-import javax.swing.JLabel;
-import javax.swing.JOptionPane;
-import javax.swing.JPanel;
-import javax.swing.JPasswordField;
import java.io.ByteArrayOutputStream;
-import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -31,19 +26,17 @@ import java.io.InputStream;
import java.security.Security;
import java.util.Arrays;
import java.util.Iterator;
-import java.util.MissingResourceException;
import java.util.Properties;
import java.util.Scanner;
import java.util.stream.Collectors;
public class GpgUtil {
- private static String dirPath;
- private static String secretFile;
+ private final String dirPath;
+ private final String secretFile;
private static final String GPG_EXTENSION = ".gpg";
- public GpgUtil() {
- Properties properties = loadProperties();
+ public GpgUtil(Properties properties) {
dirPath = properties.getProperty("dir.path");
secretFile = properties.getProperty("secret.file");
if (dirPath == null | secretFile == null)
@@ -70,46 +63,7 @@ public class GpgUtil {
.orElseThrow(() -> new RuntimeException("File not found"));
}
- return decryptFile(new FileInputStream(file.getAbsolutePath()), getPassword());
- }
-
- public static char[] getPassword() {
- String prompt = "Enter password";
- Console cons = System.console();
- char[] pwd;
- if (cons != null) {
- System.out.println(prompt);
- pwd = cons.readPassword();
- } else {
- JPanel panel = new JPanel();
- final JPasswordField passwordField = new JPasswordField(10);
- panel.add(new JLabel("Password"));
- panel.add(passwordField);
- JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
- @Override
- public void selectInitialValue() {
- passwordField.requestFocusInWindow();
- }
- };
- pane.createDialog(null, prompt).setVisible(true);
- pwd = passwordField.getPassword().length == 0 ? new char[0] : passwordField.getPassword();
- }
- return pwd;
- }
-
-
- private Properties loadProperties() {
- final Properties properties = new Properties();
- InputStream inputStream = getClass().getResourceAsStream("/application.properties");
- try {
- if (inputStream == null) {
- throw new IOException();
- }
- properties.load(inputStream);
- } catch (IOException e) {
- throw new MissingResourceException("Missing application properties", Properties.class.getSimpleName(), "application.properties");
- }
- return properties;
+ return decryptFile(new FileInputStream(file.getAbsolutePath()), PasswordReader.getPassword());
}
private PGPSecretKey readSecretKeyFromCol(InputStream in, long keyId) throws IOException, PGPException {
diff --git a/src/main/java/com/wimdupont/service/PasswordReader.java b/src/main/java/com/wimdupont/service/PasswordReader.java
@@ -0,0 +1,38 @@
+package com.wimdupont.service;
+
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JPasswordField;
+import java.io.Console;
+
+public class PasswordReader {
+
+ public static char[] getPassword() {
+ String prompt = "Enter password";
+ Console console = System.console();
+ return console == null
+ ? getPasswordByPanel(prompt)
+ : getPasswordByConsole(prompt, console);
+ }
+
+ private static char[] getPasswordByConsole(String prompt, Console console) {
+ System.out.println(prompt);
+ return console.readPassword();
+ }
+
+ private static char[] getPasswordByPanel(String prompt) {
+ JPanel panel = new JPanel();
+ final JPasswordField passwordField = new JPasswordField(10);
+ panel.add(new JLabel("Password"));
+ panel.add(passwordField);
+ JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
+ @Override
+ public void selectInitialValue() {
+ passwordField.requestFocusInWindow();
+ }
+ };
+ pane.createDialog(null, prompt).setVisible(true);
+ return passwordField.getPassword().length == 0 ? new char[0] : passwordField.getPassword();
+ }
+}
diff --git a/src/main/java/com/wimdupont/service/PropertiesLoader.java b/src/main/java/com/wimdupont/service/PropertiesLoader.java
@@ -0,0 +1,23 @@
+package com.wimdupont.service;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.MissingResourceException;
+import java.util.Properties;
+
+public class PropertiesLoader {
+
+ public Properties loadProperties() {
+ final Properties properties = new Properties();
+ InputStream inputStream = getClass().getResourceAsStream("/application.properties");
+ try {
+ if (inputStream == null) {
+ throw new IOException();
+ }
+ properties.load(inputStream);
+ } catch (IOException e) {
+ throw new MissingResourceException("Missing application properties", Properties.class.getSimpleName(), "application.properties");
+ }
+ return properties;
+ }
+}