feat: use tlv from commons

This commit is contained in:
2021-04-13 01:16:09 +08:00
parent b18e46c406
commit de3b8ac73f
4 changed files with 3 additions and 72 deletions

View File

@@ -12,7 +12,7 @@
}, },
"repo": { "repo": {
"dependencies": [ "dependencies": [
"me.hatter:commons:3.0", "me.hatter:commons:3.29",
"org.bouncycastle:bcall-jdk15on:1.60" "org.bouncycastle:bcall-jdk15on:1.60"
], ],
"testDependencies": [ "testDependencies": [

View File

@@ -1,6 +1,7 @@
package me.hatter.tools.tinyencrypt.encrypt; package me.hatter.tools.tinyencrypt.encrypt;
import me.hatter.tools.commons.security.crypt.CryptOutputStream; import me.hatter.tools.commons.security.crypt.CryptOutputStream;
import me.hatter.tools.commons.tlv.TlvUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.io.*; import java.io.*;
@@ -24,7 +25,7 @@ public class EncryptedFile extends FilterOutputStream {
public EncryptedFile(DataOutputStream rawOut, TinyEncryptMeta tinyEncryptMeta) throws IOException { public EncryptedFile(DataOutputStream rawOut, TinyEncryptMeta tinyEncryptMeta) throws IOException {
super(getCryptoOutputStream(rawOut, tinyEncryptMeta)); super(getCryptoOutputStream(rawOut, tinyEncryptMeta));
// this.tinyEncryptMeta = tinyEncryptMeta; // this.tinyEncryptMeta = tinyEncryptMeta;
TlvUtil.write(rawOut, TlvUtil.create(0, TinyEncryptMetaUtil.toString(tinyEncryptMeta))); TlvUtil.writeTlv(rawOut, TlvUtil.create(0, TinyEncryptMetaUtil.toString(tinyEncryptMeta)));
rawOut.flush(); rawOut.flush();
} }

View File

@@ -1,31 +0,0 @@
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;
}
}

View File

@@ -1,39 +0,0 @@
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;
}
}