app-starlock/lib/blue/io_protocol/io_transferPermissions.dart
2024-05-18 09:37:50 +08:00

127 lines
3.6 KiB
Dart
Executable File
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 '../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 TransferPermissionsCommand extends SenderProtocol {
String? lockID;
String? authUserID;
String? keyID;
String? oldUserID;
String? newUserID;
int? needAuthor;
List<int>? publicKey;
List<int>? privateKey;
List<int>? token;
TransferPermissionsCommand({
this.lockID,
this.authUserID,
this.keyID,
this.oldUserID,
this.newUserID,
this.needAuthor,
this.publicKey,
this.privateKey,
this.token
}) : super(CommandType.transferPermissions);
@override
String toString() {
return 'TransferPermissionsCommand{lockID: $lockID, '
'authUserID: $authUserID, keyID: $keyID, oldUserID: $oldUserID, '
'newUserID: $newUserID, 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);
//oldUserID 20
int oldUserIDLength = utf8.encode(oldUserID!).length;
data.addAll(utf8.encode(oldUserID!));
data = getFixedLengthList(data, 20 - oldUserIDLength);
//newUserID 20
int newUserIDLength = utf8.encode(newUserID!).length;
data.addAll(utf8.encode(newUserID!));
data = getFixedLengthList(data, 20 - newUserIDLength);
// 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);
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
return ebcData;
}
}
class TransferPermissionsReply extends Reply {
TransferPermissionsReply.parseData(CommandType commandType, List<int> dataDetail)
: super.parseData(commandType, dataDetail) {
data = dataDetail;
}
}