feat: add --show

This commit is contained in:
2021-06-05 00:22:46 +08:00
parent b210595909
commit 084bc7198b
8 changed files with 235 additions and 20 deletions

View File

@@ -13,6 +13,9 @@ public class TinyEncryptArgs {
@CommandLine.Option(names = {"-d", "--decrypt"}, description = "Decrypt file") @CommandLine.Option(names = {"-d", "--decrypt"}, description = "Decrypt file")
boolean decrypt = false; boolean decrypt = false;
@CommandLine.Option(names = {"-s", "--show"}, description = "Show decrypted text in window")
boolean showInWindow = false;
@CommandLine.Option(names = {"-k", "--key"}, description = "Encrypt key") @CommandLine.Option(names = {"-k", "--key"}, description = "Encrypt key")
String key; String key;

View File

@@ -164,7 +164,12 @@ public class TinyEncryptMain {
if (tinyEncryptArgs.encrypt) { if (tinyEncryptArgs.encrypt) {
result = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.compress, tinyEncryptArgs.comment); result = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.compress, tinyEncryptArgs.comment);
} else { } else {
result = EncryptedFileUtil.decryptFile(config, f); if (tinyEncryptArgs.showInWindow) {
EncryptedFileUtil.decryptInWindow(config, f);
result = false; // do not delete file
} else {
result = EncryptedFileUtil.decryptFile(config, f);
}
} }
if (result && tinyEncryptArgs.removeFile) { if (result && tinyEncryptArgs.removeFile) {
log.info("Remove file: " + f); log.info("Remove file: " + f);
@@ -176,5 +181,6 @@ public class TinyEncryptMain {
log.error(joe.getMessage()); log.error(joe.getMessage());
log.debug(joe.getMessage(), joe); log.debug(joe.getMessage(), joe);
} }
System.exit(0);
} }
} }

View File

@@ -1,7 +1,7 @@
package me.hatter.tools.tinyencrypt.config; package me.hatter.tools.tinyencrypt.config;
public class TinyEncryptConstant { public class TinyEncryptConstant {
public static final String VERSION = "0.3.1"; public static final String VERSION = "0.3.2";
public static final String ENC_FILE_EXT = ".tinyenc"; public static final String ENC_FILE_EXT = ".tinyenc";
} }

View File

@@ -11,14 +11,53 @@ import me.hatter.tools.commons.tlv.Tlv;
import me.hatter.tools.commons.tlv.TlvUtil; import me.hatter.tools.commons.tlv.TlvUtil;
import me.hatter.tools.tinyencrypt.config.TinyEncryptConfig; import me.hatter.tools.tinyencrypt.config.TinyEncryptConfig;
import me.hatter.tools.tinyencrypt.config.TinyEncryptConstant; import me.hatter.tools.tinyencrypt.config.TinyEncryptConstant;
import me.hatter.tools.tinyencrypt.win.SwingWindow;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
public class EncryptedFileUtil { public class EncryptedFileUtil {
private static final LogTool log = LogTools.getLogTool(EncryptedFileUtil.class); private static final LogTool log = LogTools.getLogTool(EncryptedFileUtil.class);
public static boolean decryptToOutputStream(TinyEncryptConfig config, File file, OutputStream os) {
try {
try (FileInputStream fis = new FileInputStream(file)) {
Tlv tlv = TlvUtil.readTlv(fis);
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
byte[] dataKey = TinyEncryptMetaUtil.decryptDataKey(config, meta);
meta.setDataKey(dataKey);
try (InputStream newIs = getDecryptInputStream(fis, meta)) {
boolean isCompressed = (meta.getCompress() != null) && meta.getCompress();
if (isCompressed) {
GZIPInputStream gzIs = new GZIPInputStream(newIs);
IOUtil.copy(gzIs, os, new DefaultRollCounter().prefix("Decrypting, "));
} else {
IOUtil.copy(newIs, os, new DefaultRollCounter().prefix("Decrypting, "));
}
os.flush();
}
}
log.info("Decrypt file success: " + file);
return true;
} catch (Exception e) {
log.error("Decrypt file filed: " + file + ", reason: " + e.getMessage());
log.debug("Decrypt file filed: " + file + ", reason: " + e.getMessage(), e);
return false;
}
}
public static void decryptInWindow(TinyEncryptConfig config, File file) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
decryptToOutputStream(config, file, baos);
SwingWindow.create("Decrypted file: " + file.getName())
.message("File: " + file)
.text(new String(baos.toByteArray(), StandardCharsets.UTF_8))
.show().getResult();
// return false;
}
public static boolean decryptFile(TinyEncryptConfig config, File file) { public static boolean decryptFile(TinyEncryptConfig config, File file) {
File decFile = getDecryptFile(file); File decFile = getDecryptFile(file);
if (decFile == null) { if (decFile == null) {
@@ -30,25 +69,9 @@ public class EncryptedFileUtil {
return false; return false;
} }
try { try {
try (FileInputStream fis = new FileInputStream(file)) { try (FileOutputStream fos = new FileOutputStream(decFile)) {
Tlv tlv = TlvUtil.readTlv(fis); return decryptToOutputStream(config, file, fos);
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
byte[] dataKey = TinyEncryptMetaUtil.decryptDataKey(config, meta);
meta.setDataKey(dataKey);
try (InputStream newIs = getDecryptInputStream(fis, meta)) {
try (FileOutputStream fos = new FileOutputStream(decFile)) {
boolean isCompressed = (meta.getCompress() != null) && meta.getCompress();
if (isCompressed) {
GZIPInputStream gzIs = new GZIPInputStream(newIs);
IOUtil.copy(gzIs, fos, new DefaultRollCounter().prefix("Decrypting, "));
} else {
IOUtil.copy(newIs, fos, new DefaultRollCounter().prefix("Decrypting, "));
}
}
}
} }
log.info("Decrypt file success: " + file);
return true;
} catch (Exception e) { } catch (Exception e) {
log.error("Decrypt file filed: " + file + ", reason: " + e.getMessage()); log.error("Decrypt file filed: " + file + ", reason: " + e.getMessage());
log.debug("Decrypt file filed: " + file + ", reason: " + e.getMessage(), e); log.debug("Decrypt file filed: " + file + ", reason: " + e.getMessage(), e);

View File

@@ -0,0 +1,34 @@
package me.hatter.tools.tinyencrypt.win;
import me.hatter.tools.commons.classloader.ClassLoaderUtil;
import me.hatter.tools.commons.io.RResource;
import me.hatter.tools.commons.reflect.ReflectUtil;
import java.awt.*;
import java.lang.reflect.Method;
public class MacDockerHelper {
static final String COM_APPLE_EAWT_APPLICATION = "com.apple.eawt.Application";
static {
if (ClassLoaderUtil.isClassCanBeLoad(COM_APPLE_EAWT_APPLICATION)) {
try {
Class<?> comAppleEawtApplication = ClassLoaderUtil.loadClass(COM_APPLE_EAWT_APPLICATION);
Method getApplicationMetohd = ReflectUtil.getDeclaredMethod(comAppleEawtApplication, "getApplication", new Class[0]);
Object application = ReflectUtil.invokeMethod(getApplicationMetohd, null, new Object[0]);
byte[] iconBytes = RResource.from(SwingWindow.class.getClassLoader()).rStream("icon.png").bytesAndClose();
Image image = Toolkit.getDefaultToolkit().createImage(iconBytes);
Method setDockIconImageMethod = ReflectUtil.getDeclaredMethod(comAppleEawtApplication, "setDockIconImage", new Class[]{Image.class});
ReflectUtil.invokeMethod(setDockIconImageMethod, application, new Object[]{image});
} catch (Error e) {
e.printStackTrace();
// IGNORE
}
} else {
System.setProperty("java.awt.headless", "true");
}
}
public static void init() {
}
}

View File

@@ -0,0 +1,115 @@
package me.hatter.tools.tinyencrypt.win;
import me.hatter.tools.commons.string.StringUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.util.concurrent.CountDownLatch;
public class SwingWindow {
private String title;
private String text;
private String message;
private boolean editable;
private String result = null;
private CountDownLatch countDownLatch;
static {
try {
MacDockerHelper.init();
} catch (Error e) {
// IGNORE
}
}
public SwingWindow(String title, String text, String message, boolean editable) {
this.title = title;
this.text = text;
this.message = message;
this.editable = editable;
}
public static SwingWindow create(String title) {
return new SwingWindow(title, "", "", false);
}
public SwingWindow message(String message) {
this.message = message;
return this;
}
public SwingWindow text(String text) {
this.text = text;
return this;
}
public SwingWindow editable(boolean editable) {
this.editable = editable;
return this;
}
public String getResult() {
try {
countDownLatch.await();
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public SwingWindow show() {
countDownLatch = new CountDownLatch(1);
JFrame frame = new JFrame((StringUtil.isEmpty(title) ? "" : (title + " - ")) + "Tiny Encrypt Window");
frame.addWindowListener(new WindowListenerImpl() {
@Override
public void windowClosing(WindowEvent e) {
frame.setVisible(false);
frame.dispose();
countDownLatch.countDown();
}
});
JTextArea text = new JTextArea(StringUtil.def(this.text, ""), 30, 80);
text.setWrapStyleWord(true);
text.setLineWrap(true);
text.setEditable(editable);
JScrollPane textScrollPane = new JScrollPane(text);
JLabel label = new JLabel(StringUtil.def(message, "Tiny Encrypt default message."));
JButton btnOk = null;
if (editable) {
btnOk = new JButton("OK!");
btnOk.addActionListener((e) -> {
String t = text.getText();
frame.setVisible(false);
frame.dispose();
this.result = t;
countDownLatch.countDown();
});
}
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener((e) -> {
frame.setVisible(false);
frame.dispose();
countDownLatch.countDown();
});
JPanel pane = new JPanel();
if (btnOk != null) {
pane.add(btnOk);
}
pane.add(btnCancel);
frame.getContentPane().add(label, BorderLayout.NORTH);
frame.getContentPane().add(pane, BorderLayout.SOUTH);
frame.getContentPane().add(textScrollPane, BorderLayout.CENTER);
frame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(
(dim.width / 2) - (frame.getSize().width / 2),
(dim.height / 2) - (frame.getSize().height / 2)
);
frame.setVisible(true);
return this;
}
}

View File

@@ -0,0 +1,34 @@
package me.hatter.tools.tinyencrypt.win;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowListenerImpl implements WindowListener {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}

BIN
src/main/resources/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB