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") @CommandLine.Option(names = {"-c", "--comment"}, description = "Encrypt comment")
String comment; String comment;
@CommandLine.Option(names = {"-E", "--encrypted-comment"}, description = "Encrypt comment")
String encryptedComment;
@CommandLine.Option(names = {"--compress"}, description = "Encrypt compress") @CommandLine.Option(names = {"--compress"}, description = "Encrypt compress")
boolean compress = false; boolean compress = false;

View File

@@ -76,7 +76,7 @@ public class TinyEncryptMain {
if (tinyEncryptArgs.encrypt) { if (tinyEncryptArgs.encrypt) {
decryptSuccess = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f, decryptSuccess = EncryptedFileUtil.encryptFile(config, tinyEncryptArgs.key, f,
tinyEncryptArgs.compress, !tinyEncryptArgs.skipEnvelop, tinyEncryptArgs.requireSign, tinyEncryptArgs.compress, !tinyEncryptArgs.skipEnvelop, tinyEncryptArgs.requireSign,
tinyEncryptArgs.comment); tinyEncryptArgs.comment, tinyEncryptArgs.encryptedComment);
} else { } else {
if (tinyEncryptArgs.showInWindow) { if (tinyEncryptArgs.showInWindow) {
EncryptedFileUtil.decryptInWindow(config, f, tinyEncryptArgs.pgp); 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 { private static void printOneFileInfo(File f, FileInputStream fis) throws IOException {
Tlv tlv = TlvUtil.readTlv(fis); Tlv tlv = TlvUtil.readTlv(fis);
TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class); TinyEncryptMeta meta = tlv.getValueAsBytes().asJSONObject(TinyEncryptMeta.class);
log.debug("Tiny encrypt meta: " + JSON.toJSONString(meta, true));
StringBuilder sb = new StringBuilder(256); StringBuilder sb = new StringBuilder(256);
sb.append("File Info\n").append(header("File")).append(f); sb.append("File Info\n").append(header("File")).append(f);
if ((meta.getCompress() != null) && meta.getCompress()) { if ((meta.getCompress() != null) && meta.getCompress()) {
@@ -143,6 +144,7 @@ public class TinyEncryptMainUtil {
if (StringUtil.isNotBlank(meta.getComment())) { if (StringUtil.isNotBlank(meta.getComment())) {
sb.append(header("Comment")).append(meta.getComment()).append("\n"); 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()); log.info(sb.toString());
} }

View File

@@ -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.3.12"; public static final String VERSION = "0.3.13";
public static final String ENC_FILE_EXT = ".tinyenc"; 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.io.RFile;
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.AESCryptTool;
import me.hatter.tools.commons.security.crypt.CryptInputStream; 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.security.digest.Digests; import me.hatter.tools.commons.security.digest.Digests;
@@ -68,6 +69,14 @@ public class EncryptedFileUtil {
dataKey = TinyEncryptMetaUtil.decryptDataKey(config, meta); dataKey = TinyEncryptMetaUtil.decryptDataKey(config, meta);
} }
meta.setDataKey(dataKey); 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)) { try (InputStream newIs = getDecryptInputStream(fis, meta)) {
boolean isCompressed = (meta.getCompress() != null) && meta.getCompress(); boolean isCompressed = (meta.getCompress() != null) && meta.getCompress();
if (isCompressed) { if (isCompressed) {
@@ -140,7 +149,7 @@ public class EncryptedFileUtil {
public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file, public static boolean encryptFile(TinyEncryptConfig config, String keyName, File file,
boolean compress, boolean useEnvelop, boolean requireSign, boolean compress, boolean useEnvelop, boolean requireSign,
String comment) { String comment, String encryptedComment) {
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);
@@ -151,7 +160,7 @@ public class EncryptedFileUtil {
return false; return false;
} }
try { 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.setFileLength(file.length());
meta.setFileLastModified(file.lastModified()); meta.setFileLastModified(file.lastModified());
meta.setCompress(compress); meta.setCompress(compress);

View File

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

View File

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