feat: encrypt works

This commit is contained in:
2021-04-14 00:30:40 +08:00
parent de3b8ac73f
commit 65b5e6b3cd
7 changed files with 165 additions and 47 deletions

View File

@@ -0,0 +1,29 @@
package me.hatter.tools.tinyencrypt;
import picocli.CommandLine;
import java.io.File;
@CommandLine.Command(name = "tiny-encrypt", version = "tiny-encrypt v0.1.0")
public class TinyEncryptArgs {
@CommandLine.Option(names = {"-e", "--encrypt"}, description = "Encrypt file")
boolean encrypt = false;
@CommandLine.Option(names = {"-d", "--decrypt"}, description = "Decrypt file")
boolean decrypt = false;
@CommandLine.Option(names = {"-k", "--key"}, description = "Encrypt key")
String key;
@CommandLine.Option(names = {"-c", "--comment"}, description = "Encrypt comment")
String comment;
@CommandLine.Parameters(paramLabel = "FILE", description = "Encrypt or Decrypt files")
File[] files;
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Display a help message")
boolean helpRequested = false;
@CommandLine.Option(names = {"-V", "--version"}, versionHelp = true, description = "Display a version message")
boolean versionRequested = false;
}

View File

@@ -1,7 +1,60 @@
package me.hatter.tools.tinyencrypt;
public class TinyEncryptMain {
public static void main(String[] args) {
import me.hatter.tools.commons.exception.JumpOutException;
import me.hatter.tools.commons.log.LogConfig;
import me.hatter.tools.commons.log.LogTool;
import me.hatter.tools.commons.log.LogTools;
import me.hatter.tools.tinyencrypt.encrypt.EncryptedFileUtil;
import me.hatter.tools.tinyencrypt.util.BCUtil;
import picocli.CommandLine;
import java.io.File;
public class TinyEncryptMain {
private static final LogTool log;
static {
BCUtil.init();
LogConfig.initMuteInfoMode();
log = LogTools.getLogTool(TinyEncryptMain.class);
}
public static void main(String[] args) {
TinyEncryptArgs tinyEncryptArgs = new TinyEncryptArgs();
CommandLine cmd = new CommandLine(tinyEncryptArgs);
cmd.parseArgs(args);
if (cmd.isUsageHelpRequested()) {
cmd.usage(cmd.getOut());
return;
} else if (cmd.isVersionHelpRequested()) {
cmd.printVersionHelp(cmd.getOut());
return;
}
boolean isEncrypt = tinyEncryptArgs.encrypt;
boolean isDecrypt = tinyEncryptArgs.decrypt;
if (isEncrypt && isDecrypt) {
log.error("Encrypt and decrypt flag cannot both assigned.");
return;
}
if ((!isDecrypt) && (!isEncrypt)) {
log.error("Encrypt and decrypt flag must assign one.");
return;
}
if ((tinyEncryptArgs.files == null) || (tinyEncryptArgs.files.length == 0)) {
log.error("FILE is not assigned.");
return;
}
try {
for (File f : tinyEncryptArgs.files) {
EncryptedFileUtil.encryptFile(tinyEncryptArgs.key, f, tinyEncryptArgs.comment);
}
} catch (JumpOutException joe) {
log.error(joe.getMessage());
log.debug(joe.getMessage(), joe);
}
}
}

View File

@@ -1,35 +0,0 @@
package me.hatter.tools.tinyencrypt.encrypt;
import me.hatter.tools.commons.security.crypt.CryptOutputStream;
import me.hatter.tools.commons.tlv.TlvUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.Security;
public class EncryptedFile extends FilterOutputStream {
static {
Security.addProvider(new BouncyCastleProvider());
}
// private TinyEncryptMeta tinyEncryptMeta;
public static void main(String[] args) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("aaa.enc"));
TinyEncryptMeta meta = TinyEncryptMetaUtil.create("prod_ec_key", "...");
EncryptedFile file = new EncryptedFile(dos, meta);
file.write("Hello World".getBytes(StandardCharsets.UTF_8));
file.close();
}
public EncryptedFile(DataOutputStream rawOut, TinyEncryptMeta tinyEncryptMeta) throws IOException {
super(getCryptoOutputStream(rawOut, tinyEncryptMeta));
// this.tinyEncryptMeta = tinyEncryptMeta;
TlvUtil.writeTlv(rawOut, TlvUtil.create(0, TinyEncryptMetaUtil.toString(tinyEncryptMeta)));
rawOut.flush();
}
private static OutputStream getCryptoOutputStream(OutputStream out, TinyEncryptMeta tinyEncryptMeta) {
return CryptOutputStream.gcmEncrypt(out, tinyEncryptMeta.getDataKey(), tinyEncryptMeta.getNonce());
}
}

View File

@@ -0,0 +1,58 @@
package me.hatter.tools.tinyencrypt.encrypt;
import me.hatter.tools.commons.io.CopyRollCounter;
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.CryptOutputStream;
import me.hatter.tools.commons.tlv.TlvUtil;
import me.hatter.tools.tinyencrypt.util.BCUtil;
import java.io.*;
public class EncryptedFileUtil {
private static final LogTool log = LogTools.getLogTool(EncryptedFileUtil.class);
static {
BCUtil.init();
}
public static void encryptFile(String keyName, File file, String comment) {
File encFile = getEncryptFile(file);
if (encFile.exists()) {
log.warn("File exists, skip: " + encFile);
return;
}
try {
try (FileInputStream fis = new FileInputStream(file)) {
try (FileOutputStream fos = new FileOutputStream(encFile)) {
TinyEncryptMeta meta = TinyEncryptMetaUtil.create(keyName, comment);
TlvUtil.writeTlv(fos, TlvUtil.create(1, TinyEncryptMetaUtil.toString(meta)));
fos.flush();
try (OutputStream newOs = getCryptoOutputStream(fos, meta)) {
IOUtil.copy(fis, newOs, new CopyRollCounter() {
@Override
public void count(long count, long length) {
// TODO ...
}
});
}
}
}
log.info("Encrypt file success: " + file);
} catch (Exception e) {
log.error("Encrypt file filed: " + file + ", reason: " + e.getMessage());
log.debug("Encrypt file filed: " + file + ", reason: " + e.getMessage(), e);
}
}
public static File getEncryptFile(File file) {
File absFile = file.getAbsoluteFile();
File encFile = new File(absFile.getParent(), absFile.getName() + ".tinyenc");
return encFile;
}
private static OutputStream getCryptoOutputStream(OutputStream out, TinyEncryptMeta tinyEncryptMeta) {
return CryptOutputStream.gcmEncrypt(out, tinyEncryptMeta.getDataKey(), tinyEncryptMeta.getNonce());
}
}

View File

@@ -3,6 +3,9 @@ package me.hatter.tools.tinyencrypt.encrypt;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.hatter.tools.commons.bytes.Bytes;
import me.hatter.tools.commons.exception.JumpOutException;
import me.hatter.tools.commons.log.LogTool;
import me.hatter.tools.commons.log.LogTools;
import me.hatter.tools.commons.network.HttpRequest;
import me.hatter.tools.commons.os.OSUtil;
import me.hatter.tools.commons.security.random.RandomTool;
@@ -13,6 +16,7 @@ import java.util.Base64;
import java.util.List;
public class TinyEncryptMetaUtil {
private static final LogTool log = LogTools.getLogTool(TinyEncryptMetaUtil.class);
private static final String KMS_GET_DATA_KEY = "https://hatter.ink/kms/get_data_key.json";
public static String toString(TinyEncryptMeta tinyEncryptMeta) {
@@ -26,11 +30,11 @@ public class TinyEncryptMetaUtil {
public static TinyEncryptMeta create(String name, String comment) {
List<HttpRequest.KeyValue> keyValues = new ArrayList<>();
keyValues.add(new HttpRequest.KeyValue("name", name));
log.info("Get data key from kms, key name: " + name);
Bytes response = HttpRequest.fromUrl(KMS_GET_DATA_KEY).post(keyValues);
JSONObject responseObject = response.asJSON();
if (responseObject.getIntValue("status") != 200) {
throw new RuntimeException("Get data key from kms error, status: "
throw new JumpOutException("Get data key from kms error, status: "
+ responseObject.getIntValue("status")
+ "detail: " + responseObject
);
@@ -49,10 +53,4 @@ public class TinyEncryptMetaUtil {
tinyEncryptMeta.setComment("test");
return tinyEncryptMeta;
}
public static void main(String[] args) {
TinyEncryptMeta tinyEncryptMeta = create("prod_ec_key", "");
System.out.println(JSON.toJSONString(tinyEncryptMeta, true));
}
}

View File

@@ -0,0 +1,14 @@
package me.hatter.tools.tinyencrypt.util;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
public class BCUtil {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static void init() {
}
}