v0.3.0
This commit is contained in:
@@ -19,6 +19,9 @@ public class TinyEncryptArgs {
|
||||
@CommandLine.Option(names = {"-c", "--comment"}, description = "Encrypt comment")
|
||||
String comment;
|
||||
|
||||
@CommandLine.Option(names = {"--compress"}, description = "Encrypt compress")
|
||||
boolean compress = false;
|
||||
|
||||
@CommandLine.Option(names = {"-C", "--config"}, description = "Encrypt config")
|
||||
File config;
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public class TinyEncryptMain {
|
||||
private static final LogTool log;
|
||||
|
||||
static {
|
||||
LogConfig.initMuteInfoMode();
|
||||
// LogConfig.initMuteInfoMode();
|
||||
log = LogTools.getLogTool(TinyEncryptMain.class);
|
||||
}
|
||||
|
||||
@@ -86,7 +86,11 @@ public class TinyEncryptMain {
|
||||
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: ").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");
|
||||
@@ -158,7 +162,7 @@ public class TinyEncryptMain {
|
||||
}
|
||||
boolean result;
|
||||
if (tinyEncryptArgs.encrypt) {
|
||||
result = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.comment);
|
||||
result = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, tinyEncryptArgs.compress, tinyEncryptArgs.comment);
|
||||
} else {
|
||||
result = EncryptedFileUtil.decryptFile(config, f);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt.config;
|
||||
|
||||
public class TinyEncryptConstant {
|
||||
public static final String VERSION = "0.2.1";
|
||||
public static final String VERSION = "0.3.0";
|
||||
|
||||
public static final String ENC_FILE_EXT = ".tinyenc";
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ 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;
|
||||
import me.hatter.tools.commons.security.crypt.CryptInputStream;
|
||||
import me.hatter.tools.commons.security.crypt.CryptOutputStream;
|
||||
import me.hatter.tools.commons.tlv.Tlv;
|
||||
import me.hatter.tools.commons.tlv.TlvUtil;
|
||||
@@ -12,6 +13,8 @@ import me.hatter.tools.tinyencrypt.config.TinyEncryptConfig;
|
||||
import me.hatter.tools.tinyencrypt.config.TinyEncryptConstant;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class EncryptedFileUtil {
|
||||
private static final LogTool log = LogTools.getLogTool(EncryptedFileUtil.class);
|
||||
@@ -32,9 +35,15 @@ public class EncryptedFileUtil {
|
||||
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
|
||||
byte[] dataKey = TinyEncryptMetaUtil.decryptDataKey(config, meta);
|
||||
meta.setDataKey(dataKey);
|
||||
try (FileOutputStream fos = new FileOutputStream(decFile)) {
|
||||
try (OutputStream newOs = getDecryptOutputStream(fos, meta)) {
|
||||
IOUtil.copy(fis, newOs, new DefaultRollCounter().prefix("Decrypting, "));
|
||||
try (InputStream newIs = getDecryptInputStream(fis, meta)) {
|
||||
try (FileOutputStream fos = new FileOutputStream(decFile)) {
|
||||
boolean isCompressed = (meta.getCompress() != null) && meta.getCompress();
|
||||
if (isCompressed) {
|
||||
GZIPInputStream gzIs = new GZIPInputStream(newIs);
|
||||
IOUtil.copy(gzIs, fos, new DefaultRollCounter().prefix("Decrypting, "));
|
||||
} else {
|
||||
IOUtil.copy(newIs, fos, new DefaultRollCounter().prefix("Decrypting, "));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,7 +56,7 @@ public class EncryptedFileUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file, String comment) {
|
||||
public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file, boolean compress, String comment) {
|
||||
File encFile = getEncryptFile(file);
|
||||
if (encFile == null) {
|
||||
log.warn("Cannot encrypt .tinyenc file: " + file);
|
||||
@@ -58,15 +67,24 @@ public class EncryptedFileUtil {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
TinyEncryptMeta meta = TinyEncryptMetaUtil.create(config, comment);
|
||||
meta.setFileLength(file.length());
|
||||
meta.setFileLastModified(file.lastModified());
|
||||
meta.setCompress(compress);
|
||||
Tlv tlv = TlvUtil.create(1, TinyEncryptMetaUtil.toString(meta));
|
||||
|
||||
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)));
|
||||
TlvUtil.writeTlv(fos, tlv);
|
||||
fos.flush();
|
||||
try (OutputStream newOs = getEncryptOutputStream(fos, meta)) {
|
||||
IOUtil.copy(fis, newOs, new DefaultRollCounter().prefix("Encrypting, "));
|
||||
if (compress) {
|
||||
GZIPOutputStream gzOs = new GZIPOutputStream(newOs);
|
||||
IOUtil.copy(fis, gzOs, new DefaultRollCounter().prefix("Encrypting, "));
|
||||
gzOs.finish();
|
||||
} else {
|
||||
IOUtil.copy(fis, newOs, new DefaultRollCounter().prefix("Encrypting, "));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,9 +118,9 @@ public class EncryptedFileUtil {
|
||||
return CryptOutputStream.gcmEncrypt(out, tinyEncryptMeta.getDataKey(), tinyEncryptMeta.getNonce());
|
||||
}
|
||||
|
||||
private static OutputStream getDecryptOutputStream(OutputStream out, TinyEncryptMeta tinyEncryptMeta) {
|
||||
private static InputStream getDecryptInputStream(InputStream is, TinyEncryptMeta tinyEncryptMeta) {
|
||||
AssertUtil.notNull(tinyEncryptMeta.getDataKey(), "Data key cannot be null");
|
||||
AssertUtil.notNull(tinyEncryptMeta.getNonce(), "Nonce cannot be null");
|
||||
return CryptOutputStream.gcmDecrypt(out, tinyEncryptMeta.getDataKey(), tinyEncryptMeta.getNonce());
|
||||
return CryptInputStream.gcmDecrypt(is, tinyEncryptMeta.getDataKey(), tinyEncryptMeta.getNonce());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ public class TinyEncryptMeta {
|
||||
private byte[] nonce;
|
||||
private Long fileLength;
|
||||
private Long fileLastModified;
|
||||
private Boolean compress;
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
@@ -86,6 +87,14 @@ public class TinyEncryptMeta {
|
||||
this.fileLastModified = fileLastModified;
|
||||
}
|
||||
|
||||
public Boolean getCompress() {
|
||||
return compress;
|
||||
}
|
||||
|
||||
public void setCompress(Boolean compress) {
|
||||
this.compress = compress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TinyEncryptMeta{" +
|
||||
@@ -94,6 +103,7 @@ public class TinyEncryptMeta {
|
||||
", userAgent='" + userAgent + '\'' +
|
||||
", comment='" + comment + '\'' +
|
||||
", envelop='" + envelop + '\'' +
|
||||
", compress=" + compress +
|
||||
", fileLength=" + fileLength +
|
||||
", fileLastModified=" + fileLastModified +
|
||||
", dataKey=***" +
|
||||
|
||||
Reference in New Issue
Block a user