2024-04-26 15:38:59 +08:00

82 lines
2.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:convert';
import 'package:get/get.dart';
import '../../app_settings/app_settings.dart';
import '../io_tool/io_tool.dart';
import '../sm4Encipher/sm4.dart';
import '../io_reply.dart';
import '../io_sender.dart';
import '../io_type.dart';
import 'package:crypto/crypto.dart' as crypto;
//TODO:校时
class TimingCommand extends SenderProtocol {
String? lockID;
String? userID;
int? nowTime;
List<int>? token;
int? needAuthor;
List<int>? signKey;
List<int>? privateKey;
TimingCommand({
this.lockID,
this.userID,
this.nowTime,
this.token,
this.needAuthor,
this.signKey,
this.privateKey
}) : super(CommandType.calibrationTime);
@override
List<int> messageDetail() {
List<int> data = [];
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);
//lockID 40
int lockIDLength = utf8.encode(lockID!).length;
data.addAll(utf8.encode(lockID!));
data = getFixedLengthList(data, 40 - lockIDLength);
//userID 要接受钥匙的用户的useid 20
int userIDLength = utf8.encode(userID!).length;
data.addAll(utf8.encode(userID!));
data = getFixedLengthList(data, 20 - userIDLength);
// nowTime 4
int? d1 = nowTime;
data.add((d1! & 0xff000000) >> 24);
data.add((d1 & 0xff0000) >> 16);
data.add((d1 & 0xff00) >> 8);
data.add((d1 & 0xff));
if ((data.length % 16) != 0) {
int add = (16 - data.length % 16);
for (int i = 0; i < add; i++) {
data.add(0);
}
}
AppLog.log("App发给锁的数据指令类型是:${commandType!.typeName} 加密之前数据是:$data");
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
return ebcData;
}
}
class TimingReply extends Reply {
TimingReply.parseData(CommandType commandType, List<int> dataDetail)
: super.parseData(commandType, dataDetail) {
data = dataDetail;
int status = data[2];
errorWithStstus(status);
}
}