app-starlock/lib/blue/io_protocol/io_getPrivateKey.dart

122 lines
3.4 KiB
Dart
Raw Permalink Normal View History

2023-08-09 09:32:09 +08:00
import 'dart:convert';
2024-08-08 11:00:03 +08:00
import 'package:crypto/crypto.dart' as crypto;
import 'package:star_lock/blue/blue_manage.dart';
import 'package:star_lock/blue/sm4Encipher/sm4.dart';
import 'package:star_lock/tools/dateTool.dart';
import '../io_reply.dart';
import '../io_sender.dart';
2024-08-08 11:00:03 +08:00
import '../io_tool/io_tool.dart';
import '../io_type.dart';
2023-08-09 09:32:09 +08:00
List<int> publicKeyDataList = <int>[];
2023-08-09 09:32:09 +08:00
class GetPrivateKeyCommand extends SenderProtocol {
GetPrivateKeyCommand(
{this.lockID,
this.keyID,
this.authUserID,
this.nowTime,
this.needAuthor,
this.publicKeyData})
: super(CommandType.getLockPrivateKey);
String? lockID;
String? keyID; // 钥匙ID
String? authUserID; // 钥匙授权人ID
int? nowTime;
int? needAuthor;
List<int>? publicKeyData;
@override
String toString() {
return 'GetPrivateKeyCommand{lockID: $lockID, keyID: $keyID, '
'authUserID: $authUserID, nowTime: ${DateTool().dateIntToYMDHNString(nowTime)}, '
'needAuthor: $needAuthor, publicKeyData: $publicKeyData}';
}
2023-08-09 09:32:09 +08:00
@override
List<int> messageDetail() {
publicKeyDataList = publicKeyData!;
List<int> data = <int>[];
List<int> ebcData = <int>[];
2023-08-09 15:42:26 +08:00
2023-08-10 18:56:29 +08:00
// 指令类型
2024-08-08 11:00:03 +08:00
final int type = commandType!.typeValue;
final double typeDouble = type / 256;
final int type1 = typeDouble.toInt();
final int type2 = type % 256;
2023-08-10 18:56:29 +08:00
data.add(type1);
data.add(type2);
2023-08-09 15:42:26 +08:00
// 锁id
2024-08-08 11:00:03 +08:00
final 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
2024-08-08 11:00:03 +08:00
final int keyIDLength = utf8.encode(keyID!).length;
2023-08-09 15:42:26 +08:00
data.addAll(utf8.encode(keyID!));
data = getFixedLengthList(data, 40 - keyIDLength);
2024-08-08 11:00:03 +08:00
//authUserID 20
final int authUserIDLength = utf8.encode(authUserID!).length;
2023-08-09 15:42:26 +08:00
data.addAll(utf8.encode(authUserID!));
data = getFixedLengthList(data, 20 - authUserIDLength);
//NowTime 4
data.add((nowTime! & 0xff000000) >> 24);
data.add((nowTime! & 0xff0000) >> 16);
data.add((nowTime! & 0xff00) >> 8);
2024-08-08 11:00:03 +08:00
data.add(nowTime! & 0xff);
2023-08-09 15:42:26 +08:00
if (needAuthor == 0) {
2023-08-09 09:32:09 +08:00
data.add(0);
} else {
final List<int> authCodeData = <int>[];
2023-08-10 09:52:05 +08:00
//authUserID
authCodeData.addAll(utf8.encode(authUserID!));
//KeyID
authCodeData.addAll(utf8.encode(keyID!));
2023-08-10 09:52:05 +08:00
//NowTime 4
authCodeData.add((nowTime! & 0xff000000) >> 24);
authCodeData.add((nowTime! & 0xff0000) >> 16);
authCodeData.add((nowTime! & 0xff00) >> 8);
2024-08-08 11:00:03 +08:00
authCodeData.add(nowTime! & 0xff);
2023-08-10 09:52:05 +08:00
2023-08-11 15:54:38 +08:00
authCodeData.addAll(publicKeyData!);
2023-08-10 09:52:05 +08:00
2023-08-10 18:56:29 +08:00
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
final crypto.Digest authCode = crypto.md5.convert(authCodeData);
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
if ((data.length % 16) != 0) {
2024-08-08 11:00:03 +08:00
final int add = 16 - data.length % 16;
for (int i = 0; i < add; i++) {
2023-08-10 18:56:29 +08:00
data.add(0);
}
2023-08-09 09:32:09 +08:00
}
printLog(data);
// 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
2024-08-08 11:00:03 +08:00
ebcData = SM4.encrypt(data, key: utf8.encode(BlueManage().connectDeviceName), mode: SM4CryptoMode.ECB);
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)
2023-08-09 09:32:09 +08:00
: super.parseData(commandType, dataDetail) {
data = dataDetail.sublist(2);
status = data[0];
2023-08-09 09:32:09 +08:00
}
}