feat: tiny encrypt
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package me.hatter.tools.tinyencrypt;
|
||||
|
||||
public class TinyEncryptMain {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package me.hatter.tools.tinyencrypt.config;
|
||||
|
||||
public class TinyEncryptConfig {
|
||||
private String defaultKeyName;
|
||||
|
||||
public String getDefaultKeyName() {
|
||||
return defaultKeyName;
|
||||
}
|
||||
|
||||
public void setDefaultKeyName(String defaultKeyName) {
|
||||
this.defaultKeyName = defaultKeyName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package me.hatter.tools.tinyencrypt.config;
|
||||
|
||||
public class TinyEncryptConstant {
|
||||
public static final String VERSION = "0.1.0";
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package me.hatter.tools.tinyencrypt.encrypt;
|
||||
|
||||
import me.hatter.tools.commons.security.crypt.CryptOutputStream;
|
||||
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.write(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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package me.hatter.tools.tinyencrypt.encrypt;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
public class TinyEncryptMeta {
|
||||
private String version;
|
||||
private long created;
|
||||
private String userAgent;
|
||||
private String comment;
|
||||
private String envelop;
|
||||
@JSONField(serialize = false)
|
||||
private byte[] dataKey;
|
||||
private byte[] nonce;
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public long getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(long created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public String getUserAgent() {
|
||||
return userAgent;
|
||||
}
|
||||
|
||||
public void setUserAgent(String userAgent) {
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public String getEnvelop() {
|
||||
return envelop;
|
||||
}
|
||||
|
||||
public void setEnvelop(String envelop) {
|
||||
this.envelop = envelop;
|
||||
}
|
||||
|
||||
public byte[] getDataKey() {
|
||||
return dataKey;
|
||||
}
|
||||
|
||||
public void setDataKey(byte[] dataKey) {
|
||||
this.dataKey = dataKey;
|
||||
}
|
||||
|
||||
public byte[] getNonce() {
|
||||
return nonce;
|
||||
}
|
||||
|
||||
public void setNonce(byte[] nonce) {
|
||||
this.nonce = nonce;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
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.network.HttpRequest;
|
||||
import me.hatter.tools.commons.os.OSUtil;
|
||||
import me.hatter.tools.commons.security.random.RandomTool;
|
||||
import me.hatter.tools.tinyencrypt.config.TinyEncryptConstant;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class TinyEncryptMetaUtil {
|
||||
private static final String KMS_GET_DATA_KEY = "https://hatter.ink/kms/get_data_key.json";
|
||||
|
||||
public static String toString(TinyEncryptMeta tinyEncryptMeta) {
|
||||
return JSON.toJSONString(tinyEncryptMeta);
|
||||
}
|
||||
|
||||
public static TinyEncryptMeta parse(String meta) {
|
||||
return JSON.parseObject(meta, TinyEncryptMeta.class);
|
||||
}
|
||||
|
||||
public static TinyEncryptMeta create(String name, String comment) {
|
||||
List<HttpRequest.KeyValue> keyValues = new ArrayList<>();
|
||||
keyValues.add(new HttpRequest.KeyValue("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: "
|
||||
+ responseObject.getIntValue("status")
|
||||
+ "detail: " + responseObject
|
||||
);
|
||||
}
|
||||
JSONObject responseData = responseObject.getJSONObject("data");
|
||||
byte[] dataKey = Base64.getDecoder().decode(responseData.getString("dataKey"));
|
||||
String envelop = responseData.getString("envelopJwe");
|
||||
|
||||
TinyEncryptMeta tinyEncryptMeta = new TinyEncryptMeta();
|
||||
tinyEncryptMeta.setVersion("1.0");
|
||||
tinyEncryptMeta.setCreated(System.currentTimeMillis());
|
||||
tinyEncryptMeta.setDataKey(dataKey);
|
||||
tinyEncryptMeta.setEnvelop(envelop);
|
||||
tinyEncryptMeta.setNonce(RandomTool.secureRandom().nextbytes(12));
|
||||
tinyEncryptMeta.setUserAgent("TinyEncrypt v" + TinyEncryptConstant.VERSION + "@" + OSUtil.getCurrentOS().name());
|
||||
tinyEncryptMeta.setComment("test");
|
||||
return tinyEncryptMeta;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TinyEncryptMeta tinyEncryptMeta = create("prod_ec_key", "");
|
||||
|
||||
System.out.println(JSON.toJSONString(tinyEncryptMeta, true));
|
||||
}
|
||||
}
|
||||
31
src/main/java/me/hatter/tools/tinyencrypt/encrypt/Tlv.java
Normal file
31
src/main/java/me/hatter/tools/tinyencrypt/encrypt/Tlv.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package me.hatter.tools.tinyencrypt.encrypt;
|
||||
|
||||
public class Tlv {
|
||||
private int tag;
|
||||
private int length;
|
||||
private byte[] value;
|
||||
|
||||
public int getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void setTag(int tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public byte[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(byte[] value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package me.hatter.tools.tinyencrypt.encrypt;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class TlvUtil {
|
||||
|
||||
public static void write(DataOutputStream os, Tlv tlv) throws IOException {
|
||||
os.writeShort(tlv.getTag());
|
||||
os.writeInt(tlv.getLength());
|
||||
os.write(tlv.getValue());
|
||||
}
|
||||
|
||||
public static Tlv read(DataInputStream is) throws IOException {
|
||||
int tag = is.readShort();
|
||||
int length = is.readInt();
|
||||
byte[] bs = new byte[length];
|
||||
is.readFully(bs);
|
||||
Tlv tlv = new Tlv();
|
||||
tlv.setTag(tag);
|
||||
tlv.setLength(length);
|
||||
tlv.setValue(bs);
|
||||
return tlv;
|
||||
}
|
||||
|
||||
public static Tlv create(int tag, String value) {
|
||||
return create(tag, value.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static Tlv create(int tag, byte[] value) {
|
||||
Tlv tlv = new Tlv();
|
||||
tlv.setTag(tag);
|
||||
tlv.setLength(value.length);
|
||||
tlv.setValue(value);
|
||||
return tlv;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user