feat: v0.3.13 add encrypted comment

This commit is contained in:
2022-04-03 23:24:52 +08:00
parent 7e74afe98c
commit 284d2874d6
7 changed files with 34 additions and 5 deletions

View File

@@ -25,6 +25,9 @@ public class TinyEncryptArgs {
@CommandLine.Option(names = {"-c", "--comment"}, description = "Encrypt comment")
String comment;
@CommandLine.Option(names = {"-E", "--encrypted-comment"}, description = "Encrypt comment")
String encryptedComment;
@CommandLine.Option(names = {"--compress"}, description = "Encrypt compress")
boolean compress = false;

View File

@@ -76,7 +76,7 @@ public class TinyEncryptMain {
if (tinyEncryptArgs.encrypt) {
decryptSuccess = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f,
tinyEncryptArgs.compress, !tinyEncryptArgs.skipEnvelop, tinyEncryptArgs.requireSign,
tinyEncryptArgs.comment);
tinyEncryptArgs.comment, tinyEncryptArgs.encryptedComment);
} else {
if (tinyEncryptArgs.showInWindow) {
EncryptedFileUtil.decryptInWindow(config, f, tinyEncryptArgs.pgp);

View File

@@ -117,6 +117,7 @@ public class TinyEncryptMainUtil {
private static void printOneFileInfo(File f, FileInputStream fis) throws IOException {
Tlv tlv = TlvUtil.readTlv(fis);
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
log.debug("Tiny encrypt meta: " + JSON.toJSONString(meta, true));
StringBuilder sb = new StringBuilder(256);
sb.append("File Info\n").append(header("File")).append(f);
if ((meta.getCompress() != null) && meta.getCompress()) {
@@ -143,6 +144,7 @@ public class TinyEncryptMainUtil {
if (StringUtil.isNotBlank(meta.getComment())) {
sb.append(header("Comment")).append(meta.getComment()).append("\n");
}
sb.append(header("Encrypted comment")).append(toYesOrNo(StringUtil.isNotBlank(meta.getEncryptedComment()))).append("\n");
log.info(sb.toString());
}

View File

@@ -1,7 +1,7 @@
package me.hatter.tools.tinyencrypt.config;
public class TinyEncryptConstant {
public static final String VERSION = "0.3.12";
public static final String VERSION = "0.3.13";
public static final String ENC_FILE_EXT = ".tinyenc";
}

View File

@@ -8,6 +8,7 @@ import me.hatter.tools.commons.io.IOUtil;
import me.hatter.tools.commons.io.RFile;
import me.hatter.tools.commons.log.LogTool;
import me.hatter.tools.commons.log.LogTools;
import me.hatter.tools.commons.security.crypt.AESCryptTool;
import me.hatter.tools.commons.security.crypt.CryptInputStream;
import me.hatter.tools.commons.security.crypt.CryptOutputStream;
import me.hatter.tools.commons.security.digest.Digests;
@@ -68,6 +69,14 @@ public class EncryptedFileUtil {
dataKey = TinyEncryptMetaUtil.decryptDataKey(config, meta);
}
meta.setDataKey(dataKey);
if (StringUtil.isNotEmpty(meta.getEncryptedComment())) {
Bytes decryptedComment = AESCryptTool.gcmDecrypt(meta.getDataKey(), meta.getNonce())
.from(Bytes.fromBase64(meta.getEncryptedComment()))
.toBytes();
log.info("Decrypted comment: >>> " + decryptedComment.string() + " <<<");
}
try (InputStream newIs = getDecryptInputStream(fis, meta)) {
boolean isCompressed = (meta.getCompress() != null) && meta.getCompress();
if (isCompressed) {
@@ -140,7 +149,7 @@ public class EncryptedFileUtil {
public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file,
boolean compress, boolean useEnvelop, boolean requireSign,
String comment) {
String comment, String encryptedComment) {
File encFile = getEncryptFile(file);
if (encFile == null) {
log.warn("Cannot encrypt .tinyenc file: " + file);
@@ -151,7 +160,7 @@ public class EncryptedFileUtil {
return false;
}
try {
TinyEncryptMeta meta = TinyEncryptMetaUtil.create(config, keyName, comment, useEnvelop, requireSign);
TinyEncryptMeta meta = TinyEncryptMetaUtil.create(config, keyName, comment, encryptedComment, useEnvelop, requireSign);
meta.setFileLength(file.length());
meta.setFileLastModified(file.lastModified());
meta.setCompress(compress);

View File

@@ -7,6 +7,7 @@ public class TinyEncryptMeta {
private long created;
private String userAgent;
private String comment;
private String encryptedComment;
private String pgpEnvelop;
private String pgpFingerprint;
private String envelop;
@@ -49,6 +50,14 @@ public class TinyEncryptMeta {
this.comment = comment;
}
public String getEncryptedComment() {
return encryptedComment;
}
public void setEncryptedComment(String encryptedComment) {
this.encryptedComment = encryptedComment;
}
public String getPgpEnvelop() {
return pgpEnvelop;
}
@@ -120,6 +129,7 @@ public class TinyEncryptMeta {
", created=" + created +
", userAgent='" + userAgent + '\'' +
", comment='" + comment + '\'' +
", encryptedComment='" + encryptedComment + '\'' +
", envelop='" + envelop + '\'' +
", compress=" + compress +
", fileLength=" + fileLength +

View File

@@ -89,7 +89,7 @@ public class TinyEncryptMetaUtil {
}
public static TinyEncryptMeta create(
TinyEncryptConfig config, String keyName, String comment,
TinyEncryptConfig config, String keyName, String comment, String encryptedComment,
boolean useEnvelop,
boolean requireSignature) {
PrivateKey privateKey = null;
@@ -134,6 +134,11 @@ public class TinyEncryptMetaUtil {
tinyEncryptMeta.setNonce(RandomTool.secureRandom().nextbytes(12));
tinyEncryptMeta.setUserAgent("TinyEncrypt v" + TinyEncryptConstant.VERSION + "@" + OSUtil.getCurrentOS().name());
tinyEncryptMeta.setComment(comment);
if (StringUtil.isNotEmpty(encryptedComment)) {
tinyEncryptMeta.setEncryptedComment(
AESCryptTool.gcmEncrypt(dataKey, tinyEncryptMeta.getNonce())
.from(Bytes.from(encryptedComment)).toBytes().asBase64());
}
return tinyEncryptMeta;
}