-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Jt808MsgEncryptionHandler): 支持消息加解密
- Loading branch information
Showing
29 changed files
with
858 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...rver-support/src/main/java/io/github/hylexus/jt/jt808/spec/Jt808MsgEncryptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.github.hylexus.jt.jt808.spec; | ||
|
||
import io.github.hylexus.jt.jt808.support.annotation.msg.resp.Jt808ResponseBody; | ||
import io.netty.buffer.ByteBuf; | ||
|
||
/** | ||
* @author hylexus | ||
* @see <a href="https://github.com/hylexus/jt-framework/issues/82">https://github.com/hylexus/jt-framework/issues/82</a> | ||
* @since 2.1.4 | ||
*/ | ||
public interface Jt808MsgEncryptionHandler { | ||
|
||
/** | ||
* @param header 请求头 | ||
* @param body 请求体;可能是明文也可能是密文,根据 `header` 判断 | ||
* @return 解密之后的明文 或者 原样返回 `body` | ||
* @see Jt808RequestHeader#msgBodyProps() | ||
* @see Jt808RequestHeader.Jt808MsgBodyProps#encryptionType() | ||
* @see Jt808RequestHeader.Jt808MsgBodyProps#dataEncryptionType() | ||
*/ | ||
ByteBuf decryptRequestBody(Jt808RequestHeader header, ByteBuf body); | ||
|
||
/** | ||
* @param response 本次响应的其他信息 | ||
* @param plaintextBody 明文数据;可能是完整包,也可能是一个子包 | ||
* @return 返回密文 或者 原样返回`plaintextBody` | ||
* @see Jt808ResponseBody#encryptionType() | ||
* @see Jt808Response#encryptionType(int) | ||
* @see Jt808Response#encryptionType(Jt808MsgEncryptionType) | ||
* @see Jt808MsgBuilder#encryptionType(int) | ||
* @see Jt808MsgBuilder#encryptionType(Jt808MsgEncryptionType) | ||
*/ | ||
ByteBuf encryptResponseBody(Jt808Response response, ByteBuf plaintextBody); | ||
|
||
Jt808MsgEncryptionHandler NO_OPS = new NoOps(); | ||
|
||
class NoOps implements Jt808MsgEncryptionHandler { | ||
|
||
@Override | ||
public ByteBuf decryptRequestBody(Jt808RequestHeader header, ByteBuf body) { | ||
return body; | ||
} | ||
|
||
@Override | ||
public ByteBuf encryptResponseBody(Jt808Response response, ByteBuf plaintextBody) { | ||
return plaintextBody; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...-server-support/src/main/java/io/github/hylexus/jt/jt808/spec/Jt808MsgEncryptionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.github.hylexus.jt.jt808.spec; | ||
|
||
import io.github.hylexus.jt.utils.FormatUtils; | ||
|
||
public interface Jt808MsgEncryptionType { | ||
|
||
int intValue(); | ||
|
||
default int bit10() { | ||
return this.intValue() & 0b001; | ||
} | ||
|
||
default int bit11() { | ||
return this.intValue() & 0b010; | ||
} | ||
|
||
default int bit12() { | ||
return this.intValue() & 0b100; | ||
} | ||
|
||
default boolean isEncrypted() { | ||
return this.intValue() != 0; | ||
} | ||
|
||
static Jt808MsgEncryptionType fromMsgBodyProps(int bodyPros) { | ||
// bit[10-12] 0001,1100,0000,0000(1C00)(加密类型) | ||
return new Default((bodyPros & 0x1c00) >> 10); | ||
} | ||
|
||
static Jt808MsgEncryptionType fromIntValue(int value) { | ||
return new Default(value & 0b111); | ||
} | ||
|
||
static Jt808MsgEncryptionType fromBits(int bit10, int bit11, int bit12) { | ||
return new Default( | ||
(bit10 & 0b1) | ||
| ((bit11 << 1) & 0b10) | ||
| ((bit12 << 2) & 0b100) | ||
); | ||
} | ||
|
||
|
||
class Default implements Jt808MsgEncryptionType { | ||
private final int value; | ||
|
||
public Default(int value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public int intValue() { | ||
return this.value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Default{" | ||
+ "value=" + value | ||
+ "(" + FormatUtils.toBinaryString(value, 3) + ")" | ||
+ '}'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.