v0.2.0
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package me.hatter.tools.tinyencrypt;
|
||||
|
||||
import me.hatter.tools.tinyencrypt.config.TinyEncryptConstant;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@CommandLine.Command(name = "tiny-encrypt", version = "tiny-encrypt v0.1.0")
|
||||
@CommandLine.Command(name = "tiny-encrypt", version = "tiny-encrypt v" + TinyEncryptConstant.VERSION)
|
||||
public class TinyEncryptArgs {
|
||||
@CommandLine.Option(names = {"-e", "--encrypt"}, description = "Encrypt file")
|
||||
boolean encrypt = false;
|
||||
@@ -18,12 +19,15 @@ public class TinyEncryptArgs {
|
||||
@CommandLine.Option(names = {"-c", "--comment"}, description = "Encrypt comment")
|
||||
String comment;
|
||||
|
||||
@CommandLine.Option(names = {"--config"}, description = "Encrypt config")
|
||||
@CommandLine.Option(names = {"-C", "--config"}, description = "Encrypt config")
|
||||
File config;
|
||||
|
||||
@CommandLine.Option(names = {"--remove-file"}, description = "Remove origin config")
|
||||
@CommandLine.Option(names = {"-R", "--remove-file"}, description = "Remove origin config")
|
||||
boolean removeFile = false;
|
||||
|
||||
@CommandLine.Option(names = {"-I", "--info"}, description = "Encrypt file info")
|
||||
boolean fileInfo = false;
|
||||
|
||||
@CommandLine.Parameters(paramLabel = "FILE", description = "Encrypt or Decrypt files")
|
||||
File[] files;
|
||||
|
||||
|
||||
@@ -10,13 +10,20 @@ import me.hatter.tools.commons.security.key.KeyPairTool;
|
||||
import me.hatter.tools.commons.security.key.KeyUtil;
|
||||
import me.hatter.tools.commons.security.key.PKType;
|
||||
import me.hatter.tools.commons.string.StringUtil;
|
||||
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.util.BCUtil;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.security.KeyPair;
|
||||
import java.util.Date;
|
||||
|
||||
public class TinyEncryptMain {
|
||||
private static final String DEFAULT_TINY_ENCRYPT_CONFIG = "~/.tinyencrypt_config.json";
|
||||
@@ -66,6 +73,49 @@ public class TinyEncryptMain {
|
||||
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).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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// ====================================================================================
|
||||
|
||||
boolean isEncrypt = tinyEncryptArgs.encrypt;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt.config;
|
||||
|
||||
public class TinyEncryptConstant {
|
||||
public static final String VERSION = "0.1.0";
|
||||
public static final String VERSION = "0.2.0";
|
||||
|
||||
public static final String ENC_FILE_EXT = ".tinyenc";
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt.encrypt;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import me.hatter.tools.commons.assertion.AssertUtil;
|
||||
import me.hatter.tools.commons.io.CopyRollCounter;
|
||||
import me.hatter.tools.commons.io.DefaultRollCounter;
|
||||
import me.hatter.tools.commons.io.IOUtil;
|
||||
import me.hatter.tools.commons.log.LogTool;
|
||||
import me.hatter.tools.commons.log.LogTools;
|
||||
@@ -10,6 +9,7 @@ import me.hatter.tools.commons.security.crypt.CryptOutputStream;
|
||||
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.util.BCUtil;
|
||||
|
||||
import java.io.*;
|
||||
@@ -39,12 +39,7 @@ public class EncryptedFileUtil {
|
||||
meta.setDataKey(dataKey);
|
||||
try (FileOutputStream fos = new FileOutputStream(decFile)) {
|
||||
try (OutputStream newOs = getDecryptOutputStream(fos, meta)) {
|
||||
IOUtil.copy(fis, newOs, new CopyRollCounter() {
|
||||
@Override
|
||||
public void count(long count, long length) {
|
||||
// TODO ... ProcessBar
|
||||
}
|
||||
});
|
||||
IOUtil.copy(fis, newOs, new DefaultRollCounter().prefix("Decrypting, "));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,6 +54,10 @@ public class EncryptedFileUtil {
|
||||
|
||||
public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file, String comment) {
|
||||
File encFile = getEncryptFile(file);
|
||||
if (encFile == null) {
|
||||
log.warn("Cannot encrypt .tinyenc file: " + file);
|
||||
return false;
|
||||
}
|
||||
if (encFile.exists()) {
|
||||
log.warn("File exists, skip: " + encFile);
|
||||
return false;
|
||||
@@ -67,15 +66,12 @@ public class EncryptedFileUtil {
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
try (FileOutputStream fos = new FileOutputStream(encFile)) {
|
||||
TinyEncryptMeta meta = TinyEncryptMetaUtil.create(config, comment);
|
||||
meta.setFileLength(file.length());
|
||||
meta.setFileLastModified(file.lastModified());
|
||||
TlvUtil.writeTlv(fos, TlvUtil.create(1, TinyEncryptMetaUtil.toString(meta)));
|
||||
fos.flush();
|
||||
try (OutputStream newOs = getEncryptOutputStream(fos, meta)) {
|
||||
IOUtil.copy(fis, newOs, new CopyRollCounter() {
|
||||
@Override
|
||||
public void count(long count, long length) {
|
||||
// TODO ... ProcessBar
|
||||
}
|
||||
});
|
||||
IOUtil.copy(fis, newOs, new DefaultRollCounter().prefix("Encrypting, "));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,17 +86,19 @@ public class EncryptedFileUtil {
|
||||
|
||||
public static File getEncryptFile(File file) {
|
||||
File absFile = file.getAbsoluteFile();
|
||||
File encFile = new File(absFile.getParent(), absFile.getName() + ".tinyenc");
|
||||
return encFile;
|
||||
if (absFile.getName().endsWith(TinyEncryptConstant.ENC_FILE_EXT)) {
|
||||
return null;
|
||||
}
|
||||
return new File(absFile.getParent(), absFile.getName() + TinyEncryptConstant.ENC_FILE_EXT);
|
||||
}
|
||||
|
||||
public static File getDecryptFile(File file) {
|
||||
File absFile = file.getAbsoluteFile();
|
||||
String fn = absFile.getName();
|
||||
if (!fn.endsWith(".tinyenc")) {
|
||||
if (!fn.endsWith(TinyEncryptConstant.ENC_FILE_EXT)) {
|
||||
return null;
|
||||
}
|
||||
return new File(absFile.getParent(), fn.substring(0, fn.length() - ".tinyenc".length()));
|
||||
return new File(absFile.getParent(), fn.substring(0, fn.length() - TinyEncryptConstant.ENC_FILE_EXT.length()));
|
||||
}
|
||||
|
||||
private static OutputStream getEncryptOutputStream(OutputStream out, TinyEncryptMeta tinyEncryptMeta) {
|
||||
|
||||
@@ -2,8 +2,6 @@ package me.hatter.tools.tinyencrypt.encrypt;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TinyEncryptMeta {
|
||||
private String version;
|
||||
private long created;
|
||||
@@ -13,6 +11,8 @@ public class TinyEncryptMeta {
|
||||
@JSONField(serialize = false)
|
||||
private byte[] dataKey;
|
||||
private byte[] nonce;
|
||||
private Long fileLength;
|
||||
private Long fileLastModified;
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
@@ -70,6 +70,22 @@ public class TinyEncryptMeta {
|
||||
this.nonce = nonce;
|
||||
}
|
||||
|
||||
public Long getFileLength() {
|
||||
return fileLength;
|
||||
}
|
||||
|
||||
public void setFileLength(Long fileLength) {
|
||||
this.fileLength = fileLength;
|
||||
}
|
||||
|
||||
public Long getFileLastModified() {
|
||||
return fileLastModified;
|
||||
}
|
||||
|
||||
public void setFileLastModified(Long fileLastModified) {
|
||||
this.fileLastModified = fileLastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TinyEncryptMeta{" +
|
||||
@@ -78,6 +94,8 @@ public class TinyEncryptMeta {
|
||||
", userAgent='" + userAgent + '\'' +
|
||||
", comment='" + comment + '\'' +
|
||||
", envelop='" + envelop + '\'' +
|
||||
", fileLength=" + fileLength +
|
||||
", fileLastModified=" + fileLastModified +
|
||||
", dataKey=***" +
|
||||
", nonce=***" +
|
||||
'}';
|
||||
|
||||
@@ -92,7 +92,7 @@ public class TinyEncryptMetaUtil {
|
||||
tinyEncryptMeta.setEnvelop(envelop);
|
||||
tinyEncryptMeta.setNonce(RandomTool.secureRandom().nextbytes(12));
|
||||
tinyEncryptMeta.setUserAgent("TinyEncrypt v" + TinyEncryptConstant.VERSION + "@" + OSUtil.getCurrentOS().name());
|
||||
tinyEncryptMeta.setComment("test");
|
||||
tinyEncryptMeta.setComment(comment);
|
||||
return tinyEncryptMeta;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user