app-starlock/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart
2023-08-12 18:32:49 +08:00

156 lines
4.4 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:star_lock/blue/sm4Encipher/sm4.dart';
import '../io_tool/io_tool.dart';
import 'io_reply.dart';
import 'io_sender.dart';
import 'io_type.dart';
import 'package:crypto/crypto.dart' as crypto;
class GetPrivateKeyCommand extends SenderProtocol {
String? lockID;
String? keyID; // 钥匙ID
String? authUserID; // 钥匙授权人ID
int? nowTime;
int? needAuthor;
List<int>? publicKeyData;
GetPrivateKeyCommand({
this.lockID,
this.keyID,
this.authUserID,
this.nowTime,
this.needAuthor,
this.publicKeyData
}) : super(CommandType.getLockPrivateKey);
@override
List<int> messageDetail() {
List<int> data = [];
List<int> ebcData = [];
// print("lockID${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
// 指令类型
int type = commandType!.typeValue;
double typeDouble = type / 256;
int type1 = typeDouble.toInt();
int type2 = type % 256;
data.add(type1);
data.add(type2);
// print("type:$type");
// print("type1:$type1");
// print("type2:$type2");
// 锁id
int lockIDLength = utf8.encode(lockID!).length;
data.addAll(utf8.encode(lockID!));
data = getFixedLengthList(data, 40 - lockIDLength);
//KeyID 40
int keyIDLength = utf8.encode(keyID!).length;
data.addAll(utf8.encode(keyID!));
data = getFixedLengthList(data, 40 - keyIDLength);
//authUserID 40
int authUserIDLength = utf8.encode(authUserID!).length;
data.addAll(utf8.encode(authUserID!));
data = getFixedLengthList(data, 20 - authUserIDLength);
//NowTime 4
// DateTime now = DateTime.now();
// int timestamp = now.millisecondsSinceEpoch;
var d1 = 0x11223344;
data.add((d1 & 0xff000000) >> 24);
data.add((d1 & 0xff0000) >> 16);
data.add((d1 & 0xff00) >> 8);
data.add((d1 & 0xff));
if (needAuthor == 0) {
data.add(0);
} else {
List<int> authCodeData = [];
//KeyID
authCodeData.addAll(utf8.encode(keyID!));
//authUserID
authCodeData.addAll(utf8.encode(authUserID!));
//NowTime 4
// DateTime now = DateTime.now();
// int timestamp = now.millisecondsSinceEpoch;
var d1 = 0x11223344;
authCodeData.add((d1 & 0xff000000) >> 24);
authCodeData.add((d1 & 0xff0000) >> 16);
authCodeData.add((d1 & 0xff00) >> 8);
authCodeData.add((d1 & 0xff));
// String pubKey = "";
// Storage.getData("bluePublicKey").then((res) => {
// pubKey = res
// });
// var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ==";
// print("pubKey:$pubKey");
// List<int> pubKeyData = base64.decode(pubKey.toString());
// print("pubKeyData:$pubKeyData");
authCodeData.addAll(publicKeyData!);
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
var authCode = crypto.md5.convert(authCodeData);
// print("authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}");
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);
}
}
print("SM4Data:$data");
// 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
String key = SM4.createHexKey(key: 'TMH_c3570480da8d');
ebcData = SM4.encrypt(data, key: key, mode: SM4CryptoMode.ECB);
ebcData.removeRange(ebcData.length - 16, ebcData.length);
// ebcData = utf8.encode(getSM4Str(data, "TMH_c3570480da8d"));
print("ebcData:$ebcData");
return ebcData;
}
}
class GetPrivateKeyReply extends Reply {
GetPrivateKeyReply.parseData(CommandType commandType, List<int> dataDetail)
: super.parseData(commandType, dataDetail) {
var getData = dataDetail.sublist(2);
// print("getData:$getData");
switch(getData[0]){
case 0x00:
//成功
print('获取私钥成功');
getData.removeAt(0);
List commKey = getData.sublist(0, 16);
List signKey = getData.sublist(16);
print("commKey:$commKey signKey:$signKey");
break;
case 0x07:
//无权限
print('获取私钥无权限');
break;
case 0x0f:
//用户已存在
print('获取私钥:用户已存在');
break;
default:
//失败
print('获取私钥失败');
break;
}
}
}