feat works
This commit is contained in:
@@ -21,6 +21,9 @@ public class TinyEncryptArgs {
|
|||||||
@CommandLine.Option(names = {"--config"}, description = "Encrypt config")
|
@CommandLine.Option(names = {"--config"}, description = "Encrypt config")
|
||||||
File config;
|
File config;
|
||||||
|
|
||||||
|
@CommandLine.Option(names = {"--remove-file"}, description = "Remove origin config")
|
||||||
|
boolean removeFile = false;
|
||||||
|
|
||||||
@CommandLine.Parameters(paramLabel = "FILE", description = "Encrypt or Decrypt files")
|
@CommandLine.Parameters(paramLabel = "FILE", description = "Encrypt or Decrypt files")
|
||||||
File[] files;
|
File[] files;
|
||||||
|
|
||||||
|
|||||||
@@ -105,10 +105,19 @@ public class TinyEncryptMain {
|
|||||||
int index = 1;
|
int index = 1;
|
||||||
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()) {
|
||||||
|
log.info("Skip not a file: " + f);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
boolean result;
|
||||||
if (tinyEncryptArgs.encrypt) {
|
if (tinyEncryptArgs.encrypt) {
|
||||||
EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.comment);
|
result = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.comment);
|
||||||
} else {
|
} else {
|
||||||
EncryptedFileUtil.decryptFile(config, f);
|
result = EncryptedFileUtil.decryptFile(config, f);
|
||||||
|
}
|
||||||
|
if (result && tinyEncryptArgs.removeFile) {
|
||||||
|
log.info("Remove file: " + f);
|
||||||
|
f.delete();
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,15 +21,15 @@ public class EncryptedFileUtil {
|
|||||||
BCUtil.init();
|
BCUtil.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void 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) {
|
||||||
log.warn("File is not tinyenc file, skip: " + decFile);
|
log.warn("File is not tinyenc file, skip: " + decFile);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
if (decFile.exists()) {
|
if (decFile.exists()) {
|
||||||
log.warn("File exists, skip: " + decFile);
|
log.warn("File exists, skip: " + decFile);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
try (FileInputStream fis = new FileInputStream(file)) {
|
try (FileInputStream fis = new FileInputStream(file)) {
|
||||||
@@ -49,17 +49,19 @@ public class EncryptedFileUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.info("Decrypt file success: " + file);
|
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);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void encryptFile(TinyEncryptConfig config, String keyName, File file, String comment) {
|
public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file, String comment) {
|
||||||
File encFile = getEncryptFile(file);
|
File encFile = getEncryptFile(file);
|
||||||
if (encFile.exists()) {
|
if (encFile.exists()) {
|
||||||
log.warn("File exists, skip: " + encFile);
|
log.warn("File exists, skip: " + encFile);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
try (FileInputStream fis = new FileInputStream(file)) {
|
try (FileInputStream fis = new FileInputStream(file)) {
|
||||||
@@ -78,9 +80,11 @@ public class EncryptedFileUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.info("Encrypt file success: " + file);
|
log.info("Encrypt file success: " + file);
|
||||||
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Encrypt file filed: " + file + ", reason: " + e.getMessage());
|
log.error("Encrypt file filed: " + file + ", reason: " + e.getMessage());
|
||||||
log.debug("Encrypt file filed: " + file + ", reason: " + e.getMessage(), e);
|
log.debug("Encrypt file filed: " + file + ", reason: " + e.getMessage(), e);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user