totpgenerator

Generate TOTP verification codes based on encrypted GPG files.
git clone git://git.wimdupont.com/totpgenerator.git
Log | Files | Refs | README | LICENSE

commit 35a6adb000319694245fdaadd340a929213ef2e9
parent ad16e2414da96b66fac6d99d277c18031b5491e0
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sun,  1 May 2022 19:32:20 +0200

allow filename as argument

Diffstat:
Msrc/main/java/Main.java | 3++-
Msrc/main/java/service/GpgUtil.java | 32+++++++++++++++++++-------------
2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/main/java/Main.java b/src/main/java/Main.java @@ -5,7 +5,8 @@ import java.util.Date; public class Main { public static void main(String[] args) { - System.out.println(Builder.withDefaultValues(new GpgUtil().decrypt().orElseThrow().getBytes()).generate(new Date())); + String fileArgument = args.length > 0 ? args[0] : null; + System.out.println(Builder.withDefaultValues(new GpgUtil().decrypt(fileArgument).orElseThrow().getBytes()).generate(new Date())); System.exit(0); } } diff --git a/src/main/java/service/GpgUtil.java b/src/main/java/service/GpgUtil.java @@ -50,19 +50,25 @@ public class GpgUtil { } } - public Optional<String> decrypt() { + public Optional<String> decrypt(String fileArgument) { final Scanner scanner = new Scanner(System.in); - File[] files = new File(dirPath).listFiles(); - if (files == null) throw new RuntimeException(dirPath + " contains no files"); - System.out.println("Which TOTP?"); - String fileNames = Arrays.stream(files) - .filter(f -> f.getName().contains(GPG_EXTENSION)) - .map(f -> f.getName().replace(GPG_EXTENSION, "")) - .collect(Collectors.joining(", ")); - System.out.println(fileNames); - String fileName = scanner.nextLine(); - Optional<File> file = Arrays.stream(files).filter(f -> f.getName().replace(GPG_EXTENSION, "").equals(fileName)).findAny(); - if (file.isEmpty()) throw new RuntimeException("File not found"); + File file; + if (fileArgument != null) { + file = new File(dirPath + fileArgument + GPG_EXTENSION).getAbsoluteFile(); + if (!file.exists()) throw new RuntimeException(String.format("File \"%s\" not found", file.getName())); + } else { + File[] files = new File(dirPath).listFiles(); + if (files == null || files.length < 1) throw new RuntimeException(dirPath + " contains no files"); + System.out.println("Which TOTP?"); + String fileNames = Arrays.stream(files) + .filter(f -> f.getName().contains(GPG_EXTENSION)) + .map(f -> f.getName().replace(GPG_EXTENSION, "")) + .collect(Collectors.joining(", ")); + System.out.println(fileNames); + String fileName = scanner.nextLine(); + file = Arrays.stream(files).filter(f -> f.getName().replace(GPG_EXTENSION, "").equals(fileName)).findAny() + .orElseThrow(() -> new RuntimeException("File not found")); + } System.out.println("Type in GPG password."); Console cons = System.console(); @@ -74,7 +80,7 @@ public class GpgUtil { pwd = scanner.nextLine(); } try { - return Optional.ofNullable(decryptFile(new FileInputStream(file.get().getAbsolutePath()), pwd.toCharArray())); + return Optional.ofNullable(decryptFile(new FileInputStream(file.getAbsolutePath()), pwd.toCharArray())); } catch (IOException | PGPException e) { e.printStackTrace(); return Optional.empty();