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

164 lines
4.5 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 'dart:typed_data';
import 'package:crypto/crypto.dart' as crypto;
import '../io_reply.dart';
import '../io_sender.dart';
import '../io_tool/io_tool.dart';
import '../io_type.dart';
import '../sm4Encipher/sm4.dart';
//oat升级
class VoicePackageConfigure extends SenderProtocol {
VoicePackageConfigure(
{this.lockID,
this.userID,
this.keyID,
this.platform,
this.product,
this.fwSize,
this.fwMD5,
this.needAuthor,
this.signKey,
this.privateKey,
this.token,
this.encrypt = true})
: super(CommandType.startVoicePackageConfigure);
String? lockID;
String? userID;
String? keyID;
int? platform;
int? product;
int? fwSize;
String? fwMD5;
int? needAuthor;
List<int>? signKey;
List<int>? privateKey;
List<int>? token;
bool encrypt;
@override
String toString() {
return 'VoicePackageConfigure{lockID: $lockID, userID: $userID, '
'keyID: $keyID, platform: $platform, product: $product, '
' fwSize: $fwSize, '
'fwMD5: $fwMD5, needAuthor: $needAuthor, signKey: $signKey, '
'privateKey: $privateKey, token: $token}';
}
@override
int identifierValue() {
if (encrypt) {
return super.identifierValue();
} else {
return 0x20;
}
}
@override
List<int> messageDetail() {
List<int> data = <int>[];
List<int> ebcData = <int>[];
// 指令类型
final int type = commandType!.typeValue;
final double typeDouble = type / 256;
final int type1 = typeDouble.toInt();
final int type2 = type % 256;
data.add(type1);
data.add(type2);
// 锁id 40
final int lockIDLength = utf8.encode(lockID!).length;
data.addAll(utf8.encode(lockID!));
data = getFixedLengthList(data, 40 - lockIDLength);
//userID 20
final int userIDLength = utf8.encode(userID!).length;
data.addAll(utf8.encode(userID!));
data = getFixedLengthList(data, 20 - userIDLength);
//platform 2
final int platform0 = (platform! & 0xFF00) >> 8;
final int platform1 = platform! & 0xFF;
data.add(platform0);
data.add(platform1);
//product 2
data.addAll(<int>[0, 1]); //先默认是 01
//fwSize 4
final ByteData bytes = ByteData(4); // 创建一个长度为4的字节数据
bytes.setInt32(0, fwSize!);
final List<int> byteList = bytes.buffer.asUint8List();
data.addAll(byteList);
// 创建一个16字节的字节数组
final Uint8List result = Uint8List(16);
// 将每个十六进制字符转换为4位二进制数据并将其存储到结果字节数组中
for (int i = 0; i < fwMD5!.length; i += 2) {
final String hex = fwMD5!.substring(i, i + 2);
final int byteValue = int.parse(hex, radix: 16);
result[i ~/ 2] = byteValue;
}
data.addAll(result);
// token 长度4 首次请求 Token 填 0如果锁需要鉴权 操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。 当token失效或者第一次发送的时候token为0
data.addAll(token!);
if (needAuthor == 0) {
//AuthCodeLen 1
data.add(0);
} else {
final List<int> authCodeData = <int>[];
//KeyID
authCodeData.addAll(utf8.encode(keyID!));
//UserID
authCodeData.addAll(utf8.encode(userID!));
//token 4 首次请求 Token 填 0如果锁需要鉴权操作者身份则会分配动态口令并在应答消息中返回二次请求时带上。
authCodeData.addAll(token ?? <int>[]);
authCodeData.addAll(signKey ?? <int>[]);
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
final crypto.Digest authCode = crypto.md5.convert(authCodeData);
data.add(authCode.bytes.length);
data.addAll(authCode.bytes);
}
if ((data.length % 16) != 0) {
final int add = 16 - data.length % 16;
for (int i = 0; i < add; i++) {
data.add(0);
}
}
printLog(data);
if (encrypt) {
// 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
return ebcData;
} else {
data.add(0);
return data;
}
}
}
class VoicePackageConfigureReply extends Reply {
VoicePackageConfigureReply.parseData(
CommandType commandType, List<int> dataDetail)
: super.parseData(commandType, dataDetail) {
data = dataDetail;
token = data.sublist(2, 6);
status = data[6];
errorWithStstus(status);
}
List<int> token = <int>[];
}