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

172 lines
4.9 KiB
Dart
Raw Normal View History

2023-08-30 18:36:02 +08:00
import 'dart:convert';
import 'package:crypto/crypto.dart' as crypto;
import 'package:star_lock/tools/dateTool.dart';
2024-04-26 15:38:59 +08:00
import '../../app_settings/app_settings.dart';
import '../io_reply.dart';
import '../io_sender.dart';
import '../io_tool/io_tool.dart';
import '../io_type.dart';
import '../sm4Encipher/sm4.dart';
2023-08-30 18:36:02 +08:00
/*
( ) pwd 6 0UseCountLimit 0pwd 6 0UseCountLimit 0pwdNo 255userId DeleteAll !@#
2023-08-30 18:36:02 +08:00
**/
class SenderCustomPasswordsCommand extends SenderProtocol {
SenderCustomPasswordsCommand({
this.keyID,
this.userID,
this.pwdNo,
this.operate,
this.isAdmin,
2023-08-30 18:36:02 +08:00
this.pwd,
this.useCountLimit,
this.token,
this.startTime,
this.endTime,
this.needAuthor,
2024-01-11 15:14:02 +08:00
this.signKey,
2023-08-30 18:36:02 +08:00
this.privateKey,
}) : super(CommandType.generalExtendedCommond);
String? keyID;
String? userID;
int? pwdNo;
int? operate;
int? isAdmin;
String? pwd;
int? useCountLimit;
List<int>? token;
int? startTime;
int? endTime;
int? needAuthor;
List<int>? signKey;
List<int>? privateKey;
2023-08-30 18:36:02 +08:00
@override
String toString() {
return 'SenderCustomPasswordsCommand{keyID: $keyID, userID: $userID, '
'pwdNo: $pwdNo, pwd: $pwd, useCountLimit: $useCountLimit, operate: $operate, isAdmin: $isAdmin,'
'token: $token, '
'startTime:$startTime startTimeStr: ${DateTool().dateIntToYMDHNString(startTime)}, '
'endTime:$endTime endTimeStr: ${DateTool().dateIntToYMDHNString(endTime)}, '
'needAuthor: $needAuthor, signKey: $signKey, privateKey: $privateKey}';
}
2023-08-30 18:36:02 +08:00
@override
List<int> messageDetail() {
List<int> data = [];
List<int> subData = [];
List<int> ebcData = [];
// 指令类型
// int type = commandType!.typeValue;
// double typeDouble = type / 256;
// int type1 = typeDouble.toInt();
// int type2 = type % 256;
// data.add(type1);
// data.add(type2);
data.addAll(intChangList(commandType!.typeValue));
2023-08-30 18:36:02 +08:00
// 子命令类型
data.add(3);
// keyID 40
int keyIDLength = utf8.encode(keyID!).length;
subData.addAll(utf8.encode(keyID!));
subData = getFixedLengthList(subData, 40 - keyIDLength);
//userID 20
int userIDLength = utf8.encode(userID!).length;
subData.addAll(utf8.encode(userID!));
subData = getFixedLengthList(subData, 20 - userIDLength);
// PwdNo
subData.addAll(intChangList(pwdNo!));
AppLog.log("pwdNo:$pwdNo");
2023-08-30 18:36:02 +08:00
// Operate 0:注册 1修改 2:删除 3删除全部
subData.add(operate!);
// isAdmin
subData.add(isAdmin!);
2023-08-30 18:36:02 +08:00
// pwd 20
int pwdLength = utf8.encode(pwd!).length;
subData.addAll(utf8.encode(pwd!));
subData = getFixedLengthList(subData, 20 - pwdLength);
AppLog.log("pwd:$pwd");
2023-08-30 18:36:02 +08:00
// UseCountLimit
subData.addAll(intChangList(useCountLimit!));
2023-08-30 18:36:02 +08:00
// token
subData.addAll(token!);
AppLog.log("startTime:$startTime endTime:$endTime");
2023-08-30 18:36:02 +08:00
// startTime 4
subData.add((startTime! & 0xff000000) >> 24);
subData.add((startTime! & 0xff0000) >> 16);
subData.add((startTime! & 0xff00) >> 8);
subData.add((startTime! & 0xff));
// endTime 4
subData.add((endTime! & 0xff000000) >> 24);
subData.add((endTime! & 0xff0000) >> 16);
subData.add((endTime! & 0xff00) >> 8);
subData.add((endTime! & 0xff));
if (needAuthor == 0) {
2023-08-30 18:36:02 +08:00
//AuthCodeLen 1
subData.add(0);
} else {
List<int> authCodeData = [];
//KeyID
authCodeData.addAll(utf8.encode(keyID!));
2024-01-11 15:14:02 +08:00
//authUserID
authCodeData.addAll(utf8.encode(userID!));
2023-08-30 18:36:02 +08:00
//token 4 首次请求 Token 填 0如果锁需要鉴权操作者身份则会分配动态口令并在应答消息中返回二次请求时带上。
authCodeData.addAll(token!);
2024-01-11 15:14:02 +08:00
authCodeData.addAll(signKey!);
2023-08-30 18:36:02 +08:00
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
var authCode = crypto.md5.convert(authCodeData);
subData.add(authCode.bytes.length);
subData.addAll(authCode.bytes);
}
data.add(subData.length);
data.addAll(subData);
if ((data.length % 16) != 0) {
int add = (16 - data.length % 16);
for (int i = 0; i < add; i++) {
data.add(0);
}
}
printLog(data);
2023-08-30 18:36:02 +08:00
// 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
return ebcData;
}
}
class SenderCustomPasswordsReply extends Reply {
SenderCustomPasswordsReply.parseData(
CommandType commandType, List<int> dataDetail)
2023-08-30 18:36:02 +08:00
: super.parseData(commandType, dataDetail) {
data = dataDetail;
int status = data[2];
errorWithStstus(status);
2023-08-30 18:36:02 +08:00
}
}