2023-08-09 09:32:09 +08:00
|
|
|
|
|
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
2023-08-10 09:52:05 +08:00
|
|
|
|
import '../../tools/storage.dart';
|
2023-08-09 15:42:26 +08:00
|
|
|
|
import '../io_tool/io_tool.dart';
|
2023-08-09 09:32:09 +08:00
|
|
|
|
import 'io_reply.dart';
|
|
|
|
|
|
import 'io_sender.dart';
|
|
|
|
|
|
import 'io_type.dart';
|
|
|
|
|
|
|
2023-08-10 09:52:05 +08:00
|
|
|
|
import 'package:crypto/crypto.dart' as a;
|
2023-08-10 18:56:29 +08:00
|
|
|
|
import 'package:convert/convert.dart';
|
|
|
|
|
|
import 'package:sm_crypto/sm_crypto.dart';
|
2023-08-10 09:52:05 +08:00
|
|
|
|
|
2023-08-09 09:32:09 +08:00
|
|
|
|
class GetPrivateKeyCommand extends SenderProtocol {
|
|
|
|
|
|
|
|
|
|
|
|
String? lockID;
|
2023-08-09 15:42:26 +08:00
|
|
|
|
String? keyID; // 钥匙ID
|
|
|
|
|
|
String? authUserID; // 钥匙授权人ID
|
|
|
|
|
|
int? nowTime;
|
|
|
|
|
|
int? needAuthor;
|
2023-08-09 09:32:09 +08:00
|
|
|
|
GetPrivateKeyCommand({
|
|
|
|
|
|
this.lockID,
|
2023-08-09 15:42:26 +08:00
|
|
|
|
this.keyID,
|
|
|
|
|
|
this.authUserID,
|
|
|
|
|
|
this.nowTime,
|
2023-08-10 09:52:05 +08:00
|
|
|
|
this.needAuthor,
|
2023-08-09 15:42:26 +08:00
|
|
|
|
}) : super(CommandType.getLockPrivateKey);
|
2023-08-09 09:32:09 +08:00
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
List<int> messageDetail() {
|
|
|
|
|
|
List<int> data = [];
|
2023-08-10 09:52:05 +08:00
|
|
|
|
List<int> ebcData = [];
|
2023-08-09 09:32:09 +08:00
|
|
|
|
print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
|
2023-08-09 15:42:26 +08:00
|
|
|
|
|
2023-08-10 18:56:29 +08:00
|
|
|
|
// 指令类型
|
|
|
|
|
|
int type = commandType!.typeValue;
|
|
|
|
|
|
double typeDouble = type/256;
|
|
|
|
|
|
int type1 = typeDouble.toInt();
|
|
|
|
|
|
int type2 = type%256;
|
|
|
|
|
|
data.add(type1);
|
|
|
|
|
|
data.add(type2);
|
|
|
|
|
|
print("type:$type");
|
|
|
|
|
|
print("type1:$type1");
|
|
|
|
|
|
print("type2:$type2");
|
|
|
|
|
|
|
2023-08-09 15:42:26 +08:00
|
|
|
|
// 锁id
|
|
|
|
|
|
int lockIDLength = utf8.encode(lockID!).length;
|
2023-08-09 09:32:09 +08:00
|
|
|
|
data.addAll(utf8.encode(lockID!));
|
2023-08-09 15:42:26 +08:00
|
|
|
|
data = getFixedLengthList(data, 40 - lockIDLength);
|
|
|
|
|
|
|
|
|
|
|
|
//KeyID 40
|
|
|
|
|
|
int keyIDLength = utf8.encode(keyID!).length;
|
|
|
|
|
|
data.addAll(utf8.encode(keyID!));
|
|
|
|
|
|
data = getFixedLengthList(data, 40 - keyIDLength);
|
|
|
|
|
|
|
|
|
|
|
|
//authUserID 40
|
|
|
|
|
|
int authUserIDLength = utf8.encode(authUserID!).length;
|
|
|
|
|
|
data.addAll(utf8.encode(authUserID!));
|
|
|
|
|
|
data = getFixedLengthList(data, 20 - authUserIDLength);
|
|
|
|
|
|
|
|
|
|
|
|
//NowTime 4
|
2023-08-10 09:52:05 +08:00
|
|
|
|
// DateTime now = DateTime.now();
|
|
|
|
|
|
// int timestamp = now.millisecondsSinceEpoch;
|
|
|
|
|
|
var d1 = 0x11223344;
|
|
|
|
|
|
data.add((d1 & 0xff000000) >> 24);
|
|
|
|
|
|
data.add((d1 & 0xff0000) >> 16);
|
|
|
|
|
|
data.add((d1 & 0xff00) >> 8);
|
|
|
|
|
|
data.add((d1 & 0xff));
|
2023-08-09 15:42:26 +08:00
|
|
|
|
|
|
|
|
|
|
if(needAuthor == 0){
|
2023-08-09 09:32:09 +08:00
|
|
|
|
data.add(0);
|
2023-08-09 15:42:26 +08:00
|
|
|
|
}else{
|
2023-08-10 09:52:05 +08:00
|
|
|
|
List<int> authCodeData = [];
|
|
|
|
|
|
|
2023-08-10 18:56:29 +08:00
|
|
|
|
//KeyID
|
|
|
|
|
|
// authCodeData.addAll(utf8.encode(keyID!));
|
|
|
|
|
|
//
|
|
|
|
|
|
// //authUserID
|
|
|
|
|
|
// authCodeData.addAll(utf8.encode(authUserID!));
|
|
|
|
|
|
|
2023-08-10 09:52:05 +08:00
|
|
|
|
//KeyID
|
|
|
|
|
|
authCodeData.addAll(utf8.encode(keyID!));
|
|
|
|
|
|
|
|
|
|
|
|
//authUserID
|
|
|
|
|
|
authCodeData.addAll(utf8.encode(authUserID!));
|
|
|
|
|
|
|
|
|
|
|
|
//NowTime 4
|
|
|
|
|
|
// DateTime now = DateTime.now();
|
|
|
|
|
|
// int timestamp = now.millisecondsSinceEpoch;
|
|
|
|
|
|
var d1 = 0x11223344;
|
|
|
|
|
|
authCodeData.add((d1 & 0xff000000) >> 24);
|
|
|
|
|
|
authCodeData.add((d1 & 0xff0000) >> 16);
|
|
|
|
|
|
authCodeData.add((d1 & 0xff00) >> 8);
|
|
|
|
|
|
authCodeData.add((d1 & 0xff));
|
|
|
|
|
|
|
2023-08-10 18:56:29 +08:00
|
|
|
|
// var pubKey = Storage.getData("bluePublicKey");
|
|
|
|
|
|
var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ==";
|
|
|
|
|
|
List<int> pubKeyData = base64.decode(pubKey);
|
2023-08-10 09:52:05 +08:00
|
|
|
|
authCodeData.addAll(pubKeyData);
|
|
|
|
|
|
|
2023-08-10 18:56:29 +08:00
|
|
|
|
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
2023-08-10 09:52:05 +08:00
|
|
|
|
var authCode = a.md5.convert(authCodeData);
|
2023-08-10 18:56:29 +08:00
|
|
|
|
print("authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}");
|
2023-08-10 09:52:05 +08:00
|
|
|
|
|
|
|
|
|
|
data.add(authCode.bytes.length);
|
|
|
|
|
|
data.addAll(authCode.bytes);
|
2023-08-10 18:56:29 +08:00
|
|
|
|
}
|
2023-08-09 15:42:26 +08:00
|
|
|
|
|
2023-08-10 18:56:29 +08:00
|
|
|
|
if((data.length % 16) != 0){
|
|
|
|
|
|
int add = (16 - data.length % 16);
|
|
|
|
|
|
for(int i = 0; i<add; i++){
|
|
|
|
|
|
data.add(0);
|
|
|
|
|
|
}
|
2023-08-09 09:32:09 +08:00
|
|
|
|
}
|
2023-08-10 18:56:29 +08:00
|
|
|
|
print("SM4Data:$data");
|
|
|
|
|
|
// 拿到数据之后通过LockId进行SM4 ECB加密
|
|
|
|
|
|
ebcData = utf8.encode(getSM4Str(data, "TMH_c3570480da8d"));
|
|
|
|
|
|
print("ebcData:$ebcData");
|
|
|
|
|
|
// String cbcEncryptData = SM4.encrypt(data: data, key: "TMH_c3570480da8d", mode: SM4CryptoMode.CBC, iv: iv,);
|
2023-08-10 09:52:05 +08:00
|
|
|
|
return ebcData;
|
2023-08-09 09:32:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class GetPrivateKeyReply extends Reply {
|
|
|
|
|
|
GetPrivateKeyReply.parseData(CommandType commandType, List<int> dataDetail)
|
|
|
|
|
|
: super.parseData(commandType, dataDetail) {
|
|
|
|
|
|
print('获取私钥');
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
// while(index < endIndex){
|
|
|
|
|
|
// commandKey = byteUInt8(dataDetail, index);
|
|
|
|
|
|
// index += offset_1;
|
|
|
|
|
|
// switch(commandKey){
|
|
|
|
|
|
//
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|