feat: 0.5.0, add create text from window

This commit is contained in:
2022-04-17 17:18:31 +08:00
parent f0e521142f
commit e9d97a09b9
5 changed files with 62 additions and 10 deletions

View File

@@ -10,6 +10,9 @@ public class TinyEncryptArgs {
@CommandLine.Option(names = {"-e", "--encrypt"}, description = "Encrypt file") @CommandLine.Option(names = {"-e", "--encrypt"}, description = "Encrypt file")
boolean encrypt = false; boolean encrypt = false;
@CommandLine.Option(names = {"--create"}, description = "Create file")
boolean create = false;
@CommandLine.Option(names = {"-d", "--decrypt"}, description = "Decrypt file") @CommandLine.Option(names = {"-d", "--decrypt"}, description = "Decrypt file")
boolean decrypt = false; boolean decrypt = false;

View File

@@ -44,14 +44,15 @@ public class TinyEncryptMain {
} }
// ==================================================================================== // ====================================================================================
boolean isCreate = tinyEncryptArgs.create;
boolean isEncrypt = tinyEncryptArgs.encrypt; boolean isEncrypt = tinyEncryptArgs.encrypt;
boolean isDecrypt = tinyEncryptArgs.decrypt; boolean isDecrypt = tinyEncryptArgs.decrypt;
if (isEncrypt && isDecrypt) { if ((isEncrypt && isDecrypt) || (isCreate && isEncrypt) || (isCreate && isDecrypt)) {
log.error("Encrypt and decrypt flag cannot both assigned."); log.error("Encrypt create or decrypt flag cannot multiple assigned.");
return; return;
} }
if ((!isDecrypt) && (!isEncrypt)) { if ((!isDecrypt) && (!isEncrypt) && (!isCreate)) {
log.error("Encrypt and decrypt flag must assign one."); log.error("Encrypt, create and decrypt flag must assign one.");
return; return;
} }
@@ -63,9 +64,15 @@ public class TinyEncryptMain {
log.error("FILE is not assigned."); log.error("FILE is not assigned.");
return; return;
} }
int total = tinyEncryptArgs.files.length;
try { try {
if (isCreate) {
TinyEncryptMainUtil.crateFileFromWindow(tinyEncryptArgs, config);
throw new JumpOutException();
}
int index = 1; int index = 1;
int total = tinyEncryptArgs.files.length;
for (File f : tinyEncryptArgs.files) { for (File f : tinyEncryptArgs.files) {
log.info("Start processing file: " + f + ", " + index + " of " + total); log.info("Start processing file: " + f + ", " + index + " of " + total);
if (!f.isFile()) { if (!f.isFile()) {

View File

@@ -1,6 +1,7 @@
package me.hatter.tools.tinyencrypt; package me.hatter.tools.tinyencrypt;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import me.hatter.tools.commons.assertion.AssertUtil;
import me.hatter.tools.commons.bytes.ByteUtil; import me.hatter.tools.commons.bytes.ByteUtil;
import me.hatter.tools.commons.bytes.Bytes; import me.hatter.tools.commons.bytes.Bytes;
import me.hatter.tools.commons.io.RFile; import me.hatter.tools.commons.io.RFile;
@@ -17,12 +18,14 @@ 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.encrypt.EncryptedFileUtil;
import me.hatter.tools.tinyencrypt.encrypt.TinyEncryptMeta; import me.hatter.tools.tinyencrypt.encrypt.TinyEncryptMeta;
import me.hatter.tools.tinyencrypt.encrypt.TinyEncryptMetaUtil;
import me.hatter.tools.tinyencrypt.util.CardCliUtil; import me.hatter.tools.tinyencrypt.util.CardCliUtil;
import me.hatter.tools.tinyencrypt.util.SwingWindow;
import java.io.File; import java.io.*;
import java.io.FileInputStream; import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.security.KeyPair; import java.security.KeyPair;
import java.util.Date; import java.util.Date;
import java.util.Optional; import java.util.Optional;
@@ -114,6 +117,45 @@ public class TinyEncryptMainUtil {
return config; return config;
} }
public static void crateFileFromWindow(TinyEncryptArgs tinyEncryptArgs, TinyEncryptConfig config) {
if (tinyEncryptArgs.files.length != 1) {
log.error("Create only can assign one file");
return;
}
File file = tinyEncryptArgs.files[0];
if (!file.getName().endsWith(TinyEncryptConstant.ENC_FILE_EXT)) {
file = EncryptedFileUtil.getEncryptFile(file);
AssertUtil.notNull(file, "File cannot be null");
}
if (file.exists()) {
log.error("File: " + file + " exists, cannot create");
return;
}
String editResult = SwingWindow.create("Create file: " + file.getName())
.message("File: " + file)
.text("")
.editable(true)
.show().getResult();
byte[] bytes = editResult.getBytes(StandardCharsets.UTF_8);
TinyEncryptMeta meta = TinyEncryptMetaUtil.create(config, tinyEncryptArgs.key,
tinyEncryptArgs.comment, tinyEncryptArgs.encryptedComment,
!tinyEncryptArgs.skipEnvelop, tinyEncryptArgs.requireSign);
meta.setFileLength((long) bytes.length);
meta.setCreated(System.currentTimeMillis());
meta.setFileLastModified(System.currentTimeMillis());
meta.setCompress(tinyEncryptArgs.compress);
InputStream inputStream = new ByteArrayInputStream(bytes);
try {
EncryptedFileUtil.encryptFromInputStream(inputStream, file, meta);
log.info("Create file: " + file + " success");
} catch (IOException e) {
log.error("Create file: " + file + " failed", e);
}
}
private static void printOneFileInfo(File f, FileInputStream fis) throws IOException { private static void printOneFileInfo(File f, FileInputStream fis) throws IOException {
Tlv tlv = TlvUtil.readTlv(fis); Tlv tlv = TlvUtil.readTlv(fis);
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class); TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);

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.4.0"; public static final String VERSION = "0.5.0";
public static final String ENC_FILE_EXT = ".tinyenc"; public static final String ENC_FILE_EXT = ".tinyenc";
} }

View File

@@ -204,7 +204,7 @@ public class EncryptedFileUtil {
} }
} }
private static void encryptFromInputStream(InputStream inputStream, File encFile, TinyEncryptMeta meta) throws IOException { public static void encryptFromInputStream(InputStream inputStream, File encFile, TinyEncryptMeta meta) throws IOException {
Tlv tlv = TlvUtil.create(1, TinyEncryptMetaUtil.toString(meta)); Tlv tlv = TlvUtil.create(1, TinyEncryptMetaUtil.toString(meta));
try (FileOutputStream fos = new FileOutputStream(encFile)) { try (FileOutputStream fos = new FileOutputStream(encFile)) {
TlvUtil.writeTlv(fos, tlv); TlvUtil.writeTlv(fos, tlv);