feat: v0.3.3, code style
This commit is contained in:
@@ -32,89 +32,122 @@ public class TinyEncryptMain {
|
||||
log = LogTools.getLogTool(TinyEncryptMain.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
private static TinyEncryptArgs parseTinyEncryptArgs(String[] args) {
|
||||
TinyEncryptArgs tinyEncryptArgs = new TinyEncryptArgs();
|
||||
CommandLine cmd = new CommandLine(tinyEncryptArgs);
|
||||
cmd.parseArgs(args);
|
||||
|
||||
if (cmd.isUsageHelpRequested()) {
|
||||
cmd.usage(cmd.getOut());
|
||||
return;
|
||||
return null;
|
||||
} else if (cmd.isVersionHelpRequested()) {
|
||||
cmd.printVersionHelp(cmd.getOut());
|
||||
return null;
|
||||
}
|
||||
return tinyEncryptArgs;
|
||||
}
|
||||
|
||||
private static void doInitConfig(TinyEncryptArgs tinyEncryptArgs) {
|
||||
if (StringUtil.isEmpty(tinyEncryptArgs.key)) {
|
||||
log.error("Default key is not assigned");
|
||||
return;
|
||||
}
|
||||
RFile writeTinyEncryptConfigRFile;
|
||||
if (tinyEncryptArgs.config != null) {
|
||||
writeTinyEncryptConfigRFile = RFile.from(tinyEncryptArgs.config);
|
||||
} else {
|
||||
writeTinyEncryptConfigRFile = RFile.from(DEFAULT_TINY_ENCRYPT_CONFIG);
|
||||
}
|
||||
if (writeTinyEncryptConfigRFile.exists()) {
|
||||
log.error("File exists: " + tinyEncryptArgs.config);
|
||||
return;
|
||||
}
|
||||
KeyPair keyPair = KeyPairTool.ins(PKType.secp256r1).generate().getKeyPair();
|
||||
TinyEncryptConfig writeTinyEncryptConfig = new TinyEncryptConfig();
|
||||
writeTinyEncryptConfig.setDefaultKeyName(tinyEncryptArgs.key);
|
||||
writeTinyEncryptConfig.setLocalPublicKeyPem(KeyUtil.serializePublicKeyToPEM(keyPair.getPublic()));
|
||||
writeTinyEncryptConfig.setLocalPrivateKeyPem(KeyUtil.serializePrivateKeyToPEM(keyPair.getPrivate()));
|
||||
writeTinyEncryptConfigRFile.write(JSON.toJSONString(writeTinyEncryptConfig, true));
|
||||
log.info("Write file success: " + writeTinyEncryptConfigRFile.file());
|
||||
}
|
||||
|
||||
// ====================================================================================
|
||||
if (tinyEncryptArgs.doInitConfig) {
|
||||
if (StringUtil.isEmpty(tinyEncryptArgs.key)) {
|
||||
log.error("Default key is not assigned");
|
||||
return;
|
||||
}
|
||||
RFile writeTinyEncryptConfigRFile;
|
||||
if (tinyEncryptArgs.config != null) {
|
||||
writeTinyEncryptConfigRFile = RFile.from(tinyEncryptArgs.config);
|
||||
private static void fileInfo(TinyEncryptArgs tinyEncryptArgs) {
|
||||
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
|
||||
log.error("No file assigned");
|
||||
return;
|
||||
}
|
||||
for (File f : tinyEncryptArgs.files) {
|
||||
boolean isTinyEncFile = f.getName().endsWith(TinyEncryptConstant.ENC_FILE_EXT);
|
||||
if (!isTinyEncFile) {
|
||||
log.warn("File is not tiny enc file: " + f);
|
||||
} else {
|
||||
writeTinyEncryptConfigRFile = RFile.from(DEFAULT_TINY_ENCRYPT_CONFIG);
|
||||
}
|
||||
if (writeTinyEncryptConfigRFile.exists()) {
|
||||
log.error("File exists: " + tinyEncryptArgs.config);
|
||||
return;
|
||||
}
|
||||
KeyPair keyPair = KeyPairTool.ins(PKType.secp256r1).generate().getKeyPair();
|
||||
TinyEncryptConfig writeTinyEncryptConfig = new TinyEncryptConfig();
|
||||
writeTinyEncryptConfig.setDefaultKeyName(tinyEncryptArgs.key);
|
||||
writeTinyEncryptConfig.setLocalPublicKeyPem(KeyUtil.serializePublicKeyToPEM(keyPair.getPublic()));
|
||||
writeTinyEncryptConfig.setLocalPrivateKeyPem(KeyUtil.serializePrivateKeyToPEM(keyPair.getPrivate()));
|
||||
writeTinyEncryptConfigRFile.write(JSON.toJSONString(writeTinyEncryptConfig, true));
|
||||
log.info("Write file success: " + writeTinyEncryptConfigRFile.file());
|
||||
return;
|
||||
}
|
||||
|
||||
if (tinyEncryptArgs.fileInfo) {
|
||||
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
|
||||
log.error("No file assigned");
|
||||
return;
|
||||
}
|
||||
for (File f : tinyEncryptArgs.files) {
|
||||
boolean isTinyEncFile = f.getName().endsWith(TinyEncryptConstant.ENC_FILE_EXT);
|
||||
if (!isTinyEncFile) {
|
||||
log.warn("File is not tiny enc file: " + f);
|
||||
} else {
|
||||
try {
|
||||
try (FileInputStream fis = new FileInputStream(f)) {
|
||||
Tlv tlv = TlvUtil.readTlv(fis);
|
||||
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
|
||||
StringBuilder sb = new StringBuilder(256);
|
||||
sb.append("File: ").append(f);
|
||||
if ((meta.getCompress() != null) && meta.getCompress()) {
|
||||
sb.append(" [compressed]");
|
||||
}
|
||||
sb.append("\n");
|
||||
sb.append("File version: ").append(meta.getVersion()).append("\n");
|
||||
if (meta.getFileLength() != null) {
|
||||
sb.append("File size: ").append(meta.getFileLength()).append("\n");
|
||||
}
|
||||
if (meta.getFileLastModified() != null) {
|
||||
sb.append("Last modified: ")
|
||||
.append(new Date(meta.getFileLastModified()))
|
||||
.append("\n");
|
||||
}
|
||||
sb.append("Enc file created: ")
|
||||
.append(new Date(meta.getCreated()))
|
||||
.append("\n");
|
||||
sb.append("Agent: ").append(meta.getUserAgent());
|
||||
if (StringUtil.isNotBlank(meta.getComment())) {
|
||||
sb.append("Comment: ").append(meta.getComment()).append("\n");
|
||||
}
|
||||
|
||||
log.info(sb.toString());
|
||||
try {
|
||||
try (FileInputStream fis = new FileInputStream(f)) {
|
||||
Tlv tlv = TlvUtil.readTlv(fis);
|
||||
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
|
||||
StringBuilder sb = new StringBuilder(256);
|
||||
sb.append("File: ").append(f);
|
||||
if ((meta.getCompress() != null) && meta.getCompress()) {
|
||||
sb.append(" [compressed]");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Read tiny encrypt file failed: " + e.getMessage() + ", file: " + f);
|
||||
sb.append("\n");
|
||||
sb.append("File version: ").append(meta.getVersion()).append("\n");
|
||||
if (meta.getFileLength() != null) {
|
||||
sb.append("File size: ").append(meta.getFileLength()).append("\n");
|
||||
}
|
||||
if (meta.getFileLastModified() != null) {
|
||||
sb.append("Last modified: ")
|
||||
.append(new Date(meta.getFileLastModified()))
|
||||
.append("\n");
|
||||
}
|
||||
sb.append("Enc file created: ")
|
||||
.append(new Date(meta.getCreated()))
|
||||
.append("\n");
|
||||
sb.append("Agent: ").append(meta.getUserAgent());
|
||||
if (StringUtil.isNotBlank(meta.getComment())) {
|
||||
sb.append("Comment: ").append(meta.getComment()).append("\n");
|
||||
}
|
||||
|
||||
log.info(sb.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Read tiny encrypt file failed: " + e.getMessage() + ", file: " + f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static TinyEncryptConfig loadTinyEncryptConfig(TinyEncryptArgs tinyEncryptArgs) {
|
||||
TinyEncryptConfig config;
|
||||
if (tinyEncryptArgs.config != null) {
|
||||
config = RFile.from(tinyEncryptArgs.config).parseJSONObject(TinyEncryptConfig.class);
|
||||
} else {
|
||||
RFile defaultTinyEncryptConfigFile = RFile.from(DEFAULT_TINY_ENCRYPT_CONFIG);
|
||||
if (defaultTinyEncryptConfigFile.notExists()) {
|
||||
log.error("Config file not assigned, and no default config file: " + DEFAULT_TINY_ENCRYPT_CONFIG);
|
||||
return null;
|
||||
}
|
||||
config = defaultTinyEncryptConfigFile.parseJSONObject(TinyEncryptConfig.class);
|
||||
}
|
||||
if (StringUtil.isNotBlank(tinyEncryptArgs.key)) {
|
||||
log.info("Using key from args: " + tinyEncryptArgs.key);
|
||||
config.setDefaultKeyName(tinyEncryptArgs.key);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TinyEncryptArgs tinyEncryptArgs = parseTinyEncryptArgs(args);
|
||||
if (tinyEncryptArgs == null) {
|
||||
return;
|
||||
}
|
||||
// ====================================================================================
|
||||
if (tinyEncryptArgs.doInitConfig) { // --init-config
|
||||
doInitConfig(tinyEncryptArgs);
|
||||
return;
|
||||
}
|
||||
if (tinyEncryptArgs.fileInfo) { // --info
|
||||
fileInfo(tinyEncryptArgs);
|
||||
return;
|
||||
}
|
||||
// ====================================================================================
|
||||
@@ -130,20 +163,9 @@ public class TinyEncryptMain {
|
||||
return;
|
||||
}
|
||||
|
||||
TinyEncryptConfig config;
|
||||
if (tinyEncryptArgs.config != null) {
|
||||
config = RFile.from(tinyEncryptArgs.config).parseJSONObject(TinyEncryptConfig.class);
|
||||
} else {
|
||||
RFile defaultTinyEncryptConfigFile = RFile.from(DEFAULT_TINY_ENCRYPT_CONFIG);
|
||||
if (defaultTinyEncryptConfigFile.notExists()) {
|
||||
log.error("Config file not assigned, and no default config file: " + DEFAULT_TINY_ENCRYPT_CONFIG);
|
||||
return;
|
||||
}
|
||||
config = defaultTinyEncryptConfigFile.parseJSONObject(TinyEncryptConfig.class);
|
||||
}
|
||||
if (StringUtil.isNotBlank(tinyEncryptArgs.key)) {
|
||||
log.info("Using key from args: " + tinyEncryptArgs.key);
|
||||
config.setDefaultKeyName(tinyEncryptArgs.key);
|
||||
TinyEncryptConfig config = loadTinyEncryptConfig(tinyEncryptArgs);
|
||||
if (config == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
|
||||
@@ -173,7 +195,9 @@ public class TinyEncryptMain {
|
||||
}
|
||||
if (result && tinyEncryptArgs.removeFile) {
|
||||
log.info("Remove file: " + f);
|
||||
f.delete();
|
||||
if (!f.delete()) {
|
||||
log.warn("Remove file: " + f + " failed.");
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt.config;
|
||||
|
||||
public class TinyEncryptConstant {
|
||||
public static final String VERSION = "0.3.2";
|
||||
public static final String VERSION = "0.3.3";
|
||||
|
||||
public static final String ENC_FILE_EXT = ".tinyenc";
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ public class EncryptedFileUtil {
|
||||
.message("File: " + file)
|
||||
.text(new String(baos.toByteArray(), StandardCharsets.UTF_8))
|
||||
.show().getResult();
|
||||
// return false;
|
||||
}
|
||||
|
||||
public static boolean decryptFile(TinyEncryptConfig config, File file) {
|
||||
|
||||
Reference in New Issue
Block a user