feat: 0.5.0, add create text from window
This commit is contained in:
@@ -10,6 +10,9 @@ public class TinyEncryptArgs {
|
||||
@CommandLine.Option(names = {"-e", "--encrypt"}, description = "Encrypt file")
|
||||
boolean encrypt = false;
|
||||
|
||||
@CommandLine.Option(names = {"--create"}, description = "Create file")
|
||||
boolean create = false;
|
||||
|
||||
@CommandLine.Option(names = {"-d", "--decrypt"}, description = "Decrypt file")
|
||||
boolean decrypt = false;
|
||||
|
||||
|
||||
@@ -44,14 +44,15 @@ public class TinyEncryptMain {
|
||||
}
|
||||
// ====================================================================================
|
||||
|
||||
boolean isCreate = tinyEncryptArgs.create;
|
||||
boolean isEncrypt = tinyEncryptArgs.encrypt;
|
||||
boolean isDecrypt = tinyEncryptArgs.decrypt;
|
||||
if (isEncrypt && isDecrypt) {
|
||||
log.error("Encrypt and decrypt flag cannot both assigned.");
|
||||
if ((isEncrypt && isDecrypt) || (isCreate && isEncrypt) || (isCreate && isDecrypt)) {
|
||||
log.error("Encrypt create or decrypt flag cannot multiple assigned.");
|
||||
return;
|
||||
}
|
||||
if ((!isDecrypt) && (!isEncrypt)) {
|
||||
log.error("Encrypt and decrypt flag must assign one.");
|
||||
if ((!isDecrypt) && (!isEncrypt) && (!isCreate)) {
|
||||
log.error("Encrypt, create and decrypt flag must assign one.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -63,9 +64,15 @@ public class TinyEncryptMain {
|
||||
log.error("FILE is not assigned.");
|
||||
return;
|
||||
}
|
||||
int total = tinyEncryptArgs.files.length;
|
||||
|
||||
try {
|
||||
if (isCreate) {
|
||||
TinyEncryptMainUtil.crateFileFromWindow(tinyEncryptArgs, config);
|
||||
throw new JumpOutException();
|
||||
}
|
||||
|
||||
int index = 1;
|
||||
int total = tinyEncryptArgs.files.length;
|
||||
for (File f : tinyEncryptArgs.files) {
|
||||
log.info("Start processing file: " + f + ", " + index + " of " + total);
|
||||
if (!f.isFile()) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt;
|
||||
|
||||
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.Bytes;
|
||||
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.tinyencrypt.config.TinyEncryptConfig;
|
||||
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.TinyEncryptMetaUtil;
|
||||
import me.hatter.tools.tinyencrypt.util.CardCliUtil;
|
||||
import me.hatter.tools.tinyencrypt.util.SwingWindow;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPair;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
@@ -114,6 +117,45 @@ public class TinyEncryptMainUtil {
|
||||
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 {
|
||||
Tlv tlv = TlvUtil.readTlv(fis);
|
||||
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt.config;
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
try (FileOutputStream fos = new FileOutputStream(encFile)) {
|
||||
TlvUtil.writeTlv(fos, tlv);
|
||||
|
||||
Reference in New Issue
Block a user