210 lines
5.7 KiB
Dart
210 lines
5.7 KiB
Dart
|
||
import 'dart:convert';
|
||
import 'dart:typed_data';
|
||
|
||
import 'package:star_lock/tools/dateTool.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 AddUserCommand extends SenderProtocol {
|
||
String? lockID;
|
||
String? authUserID;
|
||
String? keyID;
|
||
String? userID;
|
||
int? openMode;
|
||
int? keyType;
|
||
int? startDate;
|
||
int? expireDate;
|
||
|
||
int? useCountLimit;
|
||
int? isRound;
|
||
int? weekRound;
|
||
int? startHour;
|
||
int? startMin;
|
||
int? endHour;
|
||
int? endMin;
|
||
|
||
int? role;
|
||
String? password;
|
||
int? needAuthor;
|
||
List<int>? publicKey;
|
||
List<int>? privateKey;
|
||
List<int>? token;
|
||
|
||
AddUserCommand(
|
||
{this.lockID,
|
||
this.authUserID,
|
||
this.keyID,
|
||
this.userID,
|
||
this.openMode,
|
||
this.keyType,
|
||
this.startDate,
|
||
this.expireDate,
|
||
this.useCountLimit,
|
||
this.isRound,
|
||
this.weekRound,
|
||
this.startHour,
|
||
this.startMin,
|
||
this.endHour,
|
||
this.endMin,
|
||
this.role,
|
||
this.password,
|
||
this.needAuthor,
|
||
this.publicKey,
|
||
this.privateKey,
|
||
this.token})
|
||
: super(CommandType.addUser);
|
||
|
||
@override
|
||
String toString() {
|
||
return 'AddUserCommand{lockID: $lockID, authUserID: $authUserID,'
|
||
'keyID: $keyID, userID: $userID, openMode: $openMode, '
|
||
'keyType: $keyType, '
|
||
'startDate:$startDate startDateStr:${ DateTool().dateIntToYMDHNString(startDate)} , '
|
||
'expireDate:$expireDate expireDateStr: ${DateTool().dateIntToYMDHNString(expireDate)} , '
|
||
'useCountLimit: $useCountLimit, isRound: $isRound, '
|
||
'weekRound: $weekRound, startHour: $startHour, '
|
||
'startMin: $startMin, endHour: $endHour, '
|
||
'endMin: $endMin, role: $role, password: $password, '
|
||
'needAuthor: $needAuthor, publicKey: $publicKey, '
|
||
'privateKey: $privateKey, token: $token}';
|
||
}
|
||
|
||
@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);
|
||
|
||
// 锁id 40
|
||
int lockIDLength = utf8.encode(lockID!).length;
|
||
data.addAll(utf8.encode(lockID!));
|
||
data = getFixedLengthList(data, 40 - lockIDLength);
|
||
|
||
//authUserID 20
|
||
int authUserIDLength = utf8.encode(authUserID!).length;
|
||
data.addAll(utf8.encode(authUserID!));
|
||
data = getFixedLengthList(data, 20 - authUserIDLength);
|
||
|
||
//KeyID 40
|
||
int keyIDLength = utf8.encode(keyID!).length;
|
||
data.addAll(utf8.encode(keyID!));
|
||
data = getFixedLengthList(data, 40 - keyIDLength);
|
||
|
||
//userID 要接受钥匙的用户的useid 20
|
||
int userIDLength = utf8.encode(userID!).length;
|
||
data.addAll(utf8.encode(userID!));
|
||
data = getFixedLengthList(data, 20 - userIDLength);
|
||
|
||
// openModel
|
||
data.add(openMode!);
|
||
|
||
// keyType
|
||
data.add(keyType!);
|
||
|
||
int? d1, d2;
|
||
if (role == 255) {
|
||
d1 = 0; //Date.parse(new Date()) / 1000;
|
||
d2 = 0xffffffff; //d1 + 86440;
|
||
} else {
|
||
d1 = startDate;
|
||
d2 = expireDate;
|
||
}
|
||
|
||
// StartDate 4
|
||
data.add((d1! & 0xff000000) >> 24);
|
||
data.add((d1 & 0xff0000) >> 16);
|
||
data.add((d1 & 0xff00) >> 8);
|
||
data.add((d1 & 0xff));
|
||
|
||
// expireDate 4
|
||
data.add((d2! & 0xff000000) >> 24);
|
||
data.add((d2 & 0xff0000) >> 16);
|
||
data.add((d2 & 0xff00) >> 8);
|
||
data.add((d2 & 0xff));
|
||
|
||
//useCountLimit 2
|
||
double useCountLimitDouble = useCountLimit! / 256;
|
||
int useCountLimit1 = useCountLimitDouble.toInt();
|
||
int useCountLimit2 = useCountLimit! % 256;
|
||
data.add(useCountLimit1);
|
||
data.add(useCountLimit2);
|
||
|
||
data.add(isRound!);
|
||
data.add(weekRound!);
|
||
data.add(startHour!);
|
||
data.add(startMin!);
|
||
data.add(endHour!);
|
||
data.add(endMin!);
|
||
|
||
// role 长度1 用户角色,0:普通用户,1:管理员,0xff:超级管理员
|
||
data.add(role!);
|
||
|
||
//password 超级管理员领锁时需验证该密码 20
|
||
int passwordLength = utf8.encode(password!).length;
|
||
data.addAll(utf8.encode(password!));
|
||
data = getFixedLengthList(data, 20 - passwordLength);
|
||
|
||
// token 长度4 首次请求 Token 填 0,如果锁需要鉴权 操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。 当token失效或者第一次发送的时候token为0
|
||
data.addAll(token!);
|
||
|
||
if (needAuthor == 0) {
|
||
//AuthCodeLen 1
|
||
data.add(0);
|
||
} else {
|
||
List<int> authCodeData = [];
|
||
|
||
//authUserID
|
||
authCodeData.addAll(utf8.encode(authUserID!));
|
||
|
||
//KeyID
|
||
authCodeData.addAll(utf8.encode(keyID!));
|
||
|
||
//token 4 首次请求 Token 填 0,如果锁需要鉴权操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。
|
||
authCodeData.addAll(token!);
|
||
|
||
authCodeData.addAll(publicKey!);
|
||
|
||
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
||
var authCode = crypto.md5.convert(authCodeData);
|
||
|
||
data.add(authCode.bytes.length);
|
||
data.addAll(authCode.bytes);
|
||
}
|
||
|
||
if ((data.length % 16) != 0) {
|
||
int add = (16 - data.length % 16);
|
||
for (int i = 0; i < add; i++) {
|
||
data.add(0);
|
||
}
|
||
}
|
||
|
||
printLog(data);
|
||
// 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
|
||
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
|
||
return ebcData;
|
||
}
|
||
}
|
||
|
||
class AddUserReply extends Reply {
|
||
AddUserReply.parseData(CommandType commandType, List<int> dataDetail)
|
||
: super.parseData(commandType, dataDetail) {
|
||
data = dataDetail;
|
||
status = data[46];
|
||
errorWithStstus(status);
|
||
}
|
||
}
|