totpgenerator

TOTP code generator
git clone git://git.wimdupont.com/totpgenerator.git
Log | Files | Refs | README | LICENSE

commit d817fef363749b29f3d119f3759a05fd868b51ed
parent 6b6ef474a4a9fd17a1f6858e2b102c5150ccbac1
Author: Wim Dupont <wim@wimdupont.com>
Date:   Tue, 28 Feb 2023 22:35:57 +0100

read pass with swing when no console

Diffstat:
Msrc/main/java/com/wimdupont/Main.java | 7+++++--
Msrc/main/java/com/wimdupont/service/GpgUtil.java | 32+++++++++++++++-----------------
2 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/wimdupont/Main.java b/src/main/java/com/wimdupont/Main.java @@ -2,11 +2,14 @@ package com.wimdupont; import com.bastiaanjansen.otp.TOTPGenerator; import com.wimdupont.service.GpgUtil; +import org.bouncycastle.openpgp.PGPException; + +import java.io.IOException; public class Main { - public static void main(String[] args) { + 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).orElseThrow().getBytes()).now()); + System.out.println(TOTPGenerator.withDefaultValues(new 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,6 +18,8 @@ import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder; import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider; import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory; +import javax.swing.JOptionPane; +import javax.swing.JPasswordField; import java.io.ByteArrayOutputStream; import java.io.Console; import java.io.File; @@ -28,7 +30,6 @@ import java.security.Security; import java.util.Arrays; import java.util.Iterator; import java.util.MissingResourceException; -import java.util.Optional; import java.util.Properties; import java.util.Scanner; import java.util.stream.Collectors; @@ -47,7 +48,7 @@ public class GpgUtil { throw new RuntimeException("Properties missing."); } - public Optional<String> decrypt(String fileArgument) { + public byte[] decrypt(String fileArgument) throws IOException, PGPException { final Scanner scanner = new Scanner(System.in); File file; if (fileArgument != null) { @@ -69,29 +70,26 @@ public class GpgUtil { System.out.println("Type in GPG password."); Console cons = System.console(); - String pwd; + char[] pwd; if (cons != null) { - pwd = new String(cons.readPassword()); + pwd = cons.readPassword(); } else { - System.out.println("Careful, input is not hidden"); - pwd = scanner.nextLine(); - } - try { - return Optional.ofNullable(decryptFile(new FileInputStream(file.getAbsolutePath()), pwd.toCharArray())); - } catch (IOException | PGPException e) { - e.printStackTrace(); - return Optional.empty(); + final JPasswordField passwordField = new JPasswordField(); + pwd = JOptionPane.showConfirmDialog(null, passwordField, "Enter password", + JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION + ? (passwordField.getPassword()) : new char[0]; } + return decryptFile(new FileInputStream(file.getAbsolutePath()), pwd); } private Properties loadProperties() { final Properties properties = new Properties(); - InputStream is = getClass().getResourceAsStream("/application.properties"); + InputStream inputStream = getClass().getResourceAsStream("/application.properties"); try { - if (is == null) { + if (inputStream == null) { throw new IOException(); } - properties.load(is); + properties.load(inputStream); } catch (IOException e) { throw new MissingResourceException("Missing application properties", Properties.class.getSimpleName(), "application.properties"); } @@ -108,7 +106,7 @@ public class GpgUtil { return key; } - private String decryptFile(InputStream in, char[] pass) throws IOException, PGPException { + private byte[] decryptFile(InputStream in, char[] pass) throws IOException, PGPException { Security.addProvider(new BouncyCastleProvider()); PGPSecretKey secKey; in = PGPUtil.getDecoderStream(in); @@ -144,6 +142,6 @@ public class GpgUtil { while ((ch = inLd.read()) >= 0) { bOut.write(ch); } - return bOut.toString(); + return bOut.toByteArray(); } }