feat: v0.3.3, code style
This commit is contained in:
@@ -32,89 +32,122 @@ public class TinyEncryptMain {
|
|||||||
log = LogTools.getLogTool(TinyEncryptMain.class);
|
log = LogTools.getLogTool(TinyEncryptMain.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
private static TinyEncryptArgs parseTinyEncryptArgs(String[] args) {
|
||||||
TinyEncryptArgs tinyEncryptArgs = new TinyEncryptArgs();
|
TinyEncryptArgs tinyEncryptArgs = new TinyEncryptArgs();
|
||||||
CommandLine cmd = new CommandLine(tinyEncryptArgs);
|
CommandLine cmd = new CommandLine(tinyEncryptArgs);
|
||||||
cmd.parseArgs(args);
|
cmd.parseArgs(args);
|
||||||
|
|
||||||
if (cmd.isUsageHelpRequested()) {
|
if (cmd.isUsageHelpRequested()) {
|
||||||
cmd.usage(cmd.getOut());
|
cmd.usage(cmd.getOut());
|
||||||
return;
|
return null;
|
||||||
} else if (cmd.isVersionHelpRequested()) {
|
} else if (cmd.isVersionHelpRequested()) {
|
||||||
cmd.printVersionHelp(cmd.getOut());
|
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;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
// ====================================================================================
|
private static void fileInfo(TinyEncryptArgs tinyEncryptArgs) {
|
||||||
if (tinyEncryptArgs.doInitConfig) {
|
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
|
||||||
if (StringUtil.isEmpty(tinyEncryptArgs.key)) {
|
log.error("No file assigned");
|
||||||
log.error("Default key is not assigned");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
for (File f : tinyEncryptArgs.files) {
|
||||||
RFile writeTinyEncryptConfigRFile;
|
boolean isTinyEncFile = f.getName().endsWith(TinyEncryptConstant.ENC_FILE_EXT);
|
||||||
if (tinyEncryptArgs.config != null) {
|
if (!isTinyEncFile) {
|
||||||
writeTinyEncryptConfigRFile = RFile.from(tinyEncryptArgs.config);
|
log.warn("File is not tiny enc file: " + f);
|
||||||
} else {
|
} else {
|
||||||
writeTinyEncryptConfigRFile = RFile.from(DEFAULT_TINY_ENCRYPT_CONFIG);
|
try {
|
||||||
}
|
try (FileInputStream fis = new FileInputStream(f)) {
|
||||||
if (writeTinyEncryptConfigRFile.exists()) {
|
Tlv tlv = TlvUtil.readTlv(fis);
|
||||||
log.error("File exists: " + tinyEncryptArgs.config);
|
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
|
||||||
return;
|
StringBuilder sb = new StringBuilder(256);
|
||||||
}
|
sb.append("File: ").append(f);
|
||||||
KeyPair keyPair = KeyPairTool.ins(PKType.secp256r1).generate().getKeyPair();
|
if ((meta.getCompress() != null) && meta.getCompress()) {
|
||||||
TinyEncryptConfig writeTinyEncryptConfig = new TinyEncryptConfig();
|
sb.append(" [compressed]");
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
sb.append("\n");
|
||||||
log.warn("Read tiny encrypt file failed: " + e.getMessage() + ", file: " + f);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
// ====================================================================================
|
// ====================================================================================
|
||||||
@@ -130,20 +163,9 @@ public class TinyEncryptMain {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
TinyEncryptConfig config;
|
TinyEncryptConfig config = loadTinyEncryptConfig(tinyEncryptArgs);
|
||||||
if (tinyEncryptArgs.config != null) {
|
if (config == null) {
|
||||||
config = RFile.from(tinyEncryptArgs.config).parseJSONObject(TinyEncryptConfig.class);
|
return;
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
|
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
|
||||||
@@ -173,7 +195,9 @@ public class TinyEncryptMain {
|
|||||||
}
|
}
|
||||||
if (result && tinyEncryptArgs.removeFile) {
|
if (result && tinyEncryptArgs.removeFile) {
|
||||||
log.info("Remove file: " + f);
|
log.info("Remove file: " + f);
|
||||||
f.delete();
|
if (!f.delete()) {
|
||||||
|
log.warn("Remove file: " + f + " failed.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.2";
|
public static final String VERSION = "0.3.3";
|
||||||
|
|
||||||
public static final String ENC_FILE_EXT = ".tinyenc";
|
public static final String ENC_FILE_EXT = ".tinyenc";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ public class EncryptedFileUtil {
|
|||||||
.message("File: " + file)
|
.message("File: " + file)
|
||||||
.text(new String(baos.toByteArray(), StandardCharsets.UTF_8))
|
.text(new String(baos.toByteArray(), StandardCharsets.UTF_8))
|
||||||
.show().getResult();
|
.show().getResult();
|
||||||
// return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean decryptFile(TinyEncryptConfig config, File file) {
|
public static boolean decryptFile(TinyEncryptConfig config, File file) {
|
||||||
|
|||||||
Reference in New Issue
Block a user