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