Files
2026-04-29 14:10:10 +08:00

439 lines
14 KiB
C++

#include "LicenseManger.h"
#include "dlginputaddr.h"
#include <QFile>
#include <QMessageBox>
#include <QDate>
#include <QDateEdit>
#include <time.h>
#include <QDebug>
#include <QTime>
#include <QDate>
#include "qnamespace.h"
#include <QtCore/QCoreApplication>
#include <QCryptographicHash>
MACHINE_INFO_t machine_info;
LICENSE_DATA_t license_data;
QMutex machine_info_mutex;
QMutex license_data_mutex;
void adjustDateAndTime(int year, int mon, int mday, int hour, int min, int sec);
LicenseManger::LicenseManger()
{
#if 1
UCHAR e_key[1024] = "dangerous IOP 0908 ";
pAes = new TAesClass;
pAes->InitializePrivateKey(16, e_key); //进行初始化
//setMachineInfo(16);
#endif
}
//set machine info
qint32 LicenseManger::setMachineInfo(quint32 serial_num)
{
//check serial number size
if(serial_num>999999)
{
return -1;
}
machine_info_mutex.lock();
QString serial;
serial.sprintf("VB%06d", serial_num);
QByteArray hash_data;
hash_data = QCryptographicHash::hash(serial.toAscii(),QCryptographicHash::Md5 );
//MACHINE_INFO_t machine_info;
machine_info.serial_number=serial_num;
memcpy(&machine_info.md5[0],hash_data.data(),MD5_LENGTH);
//qDebug()<<"org sn info"<<machine_info.serial_number;
//qDebug()<<"org machine md5 size"<<sizeof(machine_info.md5);
#if 0
QString md5;
md5.append(hash_data);
qDebug()<<"org machine md5 "<<md5;
for(int i=0;i<16;i++)
{
qDebug()<<tr("org md5[%1]").arg(i)<<machine_info.md5[i];
}
#endif
#if 1
char encrypt_buff[1024];
QFile file(MACH_INFO_FILE);
//encrypt the data then write to file
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.seek(0);
pAes->OnAesEncrypt((LPVOID)&machine_info, sizeof(machine_info), (LPVOID)encrypt_buff); //进行加密
//qDebug()<<"sn jiami:"<<encrypt_buff;
file.write((char *)encrypt_buff,(qint64)sizeof(encrypt_buff));
file.close();
#else
QFile file(MACH_INFO_FILE);
//encrypt the data then write to file
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.seek(0);
file.write((char *)&machine_info,(qint64)sizeof(machine_info));
file.close();
#endif
machine_info_mutex.unlock();
return 0;
}
// check machine info from flash is available
qint32 LicenseManger::checkMachineInfoAvailable()
{
static bool bmachine_info_availble;
QFile file(MACH_INFO_FILE);
if(!file.exists())
{
bmachine_info_availble=false;
return 0;
}
if(bmachine_info_availble)
{
return 1;
}
#if 1
machine_info_mutex.lock();
char encrypt_buff[1024];
char decrypt_buff[1024];
MACHINE_INFO_t machine_info_from_flash;
file.open(QIODevice::ReadOnly | QIODevice::Truncate);
file.read((char *)encrypt_buff,(qint64)sizeof(encrypt_buff));
pAes->OnAesUncrypt((LPVOID)encrypt_buff, (DWORD)sizeof(encrypt_buff),(LPVOID)decrypt_buff); //进行解密
//qDebug()<<"jiemi:"<<decrypt_buff;
memcpy(&machine_info_from_flash,decrypt_buff,sizeof(machine_info_from_flash));
file.close();
#else
MACHINE_INFO_t machine_info_from_flash;
file.open(QIODevice::ReadOnly | QIODevice::Truncate);
file.seek(0);
file.read((char *)&machine_info_from_flash,(qint64)sizeof(machine_info_from_flash));
file.close();
qDebug()<<"duchu:sn"<<machine_info_from_flash.serial_number;
#endif
qDebug()<<"jiemi:sn"<<machine_info_from_flash.serial_number;
QString serial_from_flash;
serial_from_flash.sprintf("VB%06d", machine_info_from_flash.serial_number);
QString md5_from_calc;
QByteArray hash_data;
hash_data = QCryptographicHash::hash(serial_from_flash.toAscii(),QCryptographicHash::Md5 );
md5_from_calc.append(hash_data);
if(!memcmp(hash_data.data(),&machine_info_from_flash.md5[0],16))
{
bmachine_info_availble=true;
memcpy(&machine_info,&machine_info_from_flash,sizeof(machine_info));
qDebug()<<"machine info is available"<<machine_info.serial_number;
machine_info_mutex.unlock();
return 1;
}
else
{
bmachine_info_availble=false;
qDebug()<<"machine info is not available";
machine_info_mutex.unlock();
return 0;
}
}
qint8 LicenseManger::getLicState()
{
qint8 lic_state = LICENSE_STATE_OK;
if(!checkMachineInfoAvailable())
{
lic_state = -LICENSE_STATE_MACH_INFO_DAMAGED;
}
else
{
if(!CheckLicDataValid())
{
lic_state = -LICENSE_STATE_LIC_DATA_DAMAGED;
}
}
return lic_state;
}
void LicenseManger::SaveLicData2File()
{
#if 0
QFile file("/lib/syst");
#else
QFile file(LIC_FILE);
qint32 encryp_len;
#endif
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
QMessageBox::warning(0,"警告",QString(QObject::tr("无法打开license文件")) );
}
else {
qDebug()<<"save license file start";
license_data_mutex.lock();
QByteArray ba_licdata((char *)&license_data,(sizeof(license_data) - sizeof(license_data.md5)));
//memcpy(ba_licdata.data(),(char *)&license_data,(sizeof(license_data) - sizeof(license_data.md5)));
QByteArray hash_data;
hash_data = QCryptographicHash::hash(ba_licdata,QCryptographicHash::Md5 );
memcpy(&license_data.md5[0],hash_data.data(),LIC_DATA_MD5_LEN);
#if 0
file.seek(0);
file.write((char *)&license_data.serial_number,(long long int)sizeof(license_data));
file.close();
#else
char encrypt_buff[1024];
file.seek(0);
encryp_len = pAes->OnAesEncrypt((LPVOID)&license_data, sizeof(license_data), (LPVOID)encrypt_buff); //进行加密
//qDebug()<<"jiami:"<<encrypt_buff;
file.write((char *)encrypt_buff,(qint64)sizeof(encrypt_buff));
file.close();
#endif
license_data_mutex.unlock();
qDebug()<<"save license file end";
}
}
void LicenseManger::ReadLicDataFromFile()
{
#if 0
QFile file("/lib/syst");
#else
QFile file(LIC_FILE);
#endif
if(!file.exists())
{
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
QMessageBox::warning(0,QObject::tr("警告"),QString(QObject::tr("无法打开license文件") ));
}
else {
//file.seek(0);
file.close();
ResetLicDataFile();
//show();
}
}
else {
if(!file.open(QIODevice::ReadOnly | QIODevice::Truncate)){
QMessageBox::warning(0,"警告",QString("无法打开license文件") );
}
else
{
license_data_mutex.lock();
qDebug()<<"read license file start";
#if 0
file.read((char *)&license_data.serial_number,(long long int)sizeof(license_data));
file.close();
#else
char encrypt_buff[1024];
char decrypt_buff[1024];
file.read((char *)encrypt_buff,(qint64)sizeof(encrypt_buff));
pAes->OnAesUncrypt((LPVOID)encrypt_buff, (DWORD)sizeof(encrypt_buff),(LPVOID)decrypt_buff); //进行解密
//qDebug()<<"jiemi:"<<decrypt_buff;
memcpy((char *)&license_data,decrypt_buff,sizeof(license_data));
file.close();
#endif
license_data_mutex.unlock();
qDebug()<<"read license file end";
}
}
}
void LicenseManger::ResetLicDataFile()
{
memset((char *)&license_data.serial_number,0,sizeof(license_data));
license_data.serial_number = 100001;
license_data.powerEn=1;
license_data.printEn =1;
license_data.enable =1;
license_data.powerMinuteCnt=0;
license_data.printCnt=0;
license_data.powerMinuteMax=1*31*18*60;// minute cnt
license_data.printMax = 0xfffff000;
curDate =QDate::currentDate();
QDate curDate1;
QDate buildDate = QLocale(QLocale::English).toDate(QString( __DATE__ ).replace(" ", " 0"), "MMM dd yyyy");
QTime buildTime = QTime::fromString( __TIME__, "hh:mm:ss" );
if(curDate<buildDate) {
curDate=buildDate;
int year = buildDate.year();
int month = buildDate.month();
int day = buildDate.day();
int hour = buildTime.hour();
int minute = buildTime.minute();
int sec = buildTime.second();
adjustDateAndTime(year, month, day, hour, minute, sec);//set system date and time
}
license_data.start =curDate.toString("yyyyMMdd").toInt(0);
license_data.nDate = license_data.start; //
//curDate1.toString("yyyyMMdd").toInt(0);
curDate1 = curDate.addMonths(1);
//curDate1.setYMD(curDate.year()+license_data.validity/12, (curDate.month()+license_data.validity)%12,curDate.day());
license_data.end = curDate1.toString("yyyyMMdd").toInt(0);
license_data.workout = 0x1;
license_data.remainingTime = 6*60*18*180; //half year,one day work 18 hours
license_data.printer_auth_flag = ~0x00;
license_data.printer_bworkout = 1;
//qDebug()<<license_data.start<<license_data.end ;
SaveLicData2File();
}
#include <time.h>
void adjustDateAndTime(int year, int mon, int mday, int hour, int min, int sec) {
time_t t;
struct tm nowtime;
nowtime.tm_sec = sec; // Seconds.[0-60] (1 leap second)
nowtime.tm_min = min; // Minutes.[0-59]
nowtime.tm_hour = hour; // Hours. [0-23]
nowtime.tm_mday = mday; // Day.[1-31]
nowtime.tm_mon = mon - 1; // Month. [0-11]
nowtime.tm_year = year - 1900; // Year- 1900.
nowtime.tm_isdst = -1; // DST.[-1/0/1]
t = mktime(&nowtime);
// qDebug()<<t<<year<<mon<<mday<<hour<<min<<sec;
#ifdef ARMUART
stime(&t);
#endif
}
int LicenseManger::CheckLicDataValid()
{
/* read data from file */
ReadLicDataFromFile();
/* check remaining time */
if(license_data.remainingTime >= 6*60*18*367)
{
qDebug()<<"license file was invalid";
return 0;
}
if(license_data.vernon_bach_version != VERNONBACH_VERSION_CODE)
{
qDebug()<<"license file was invalid";
return 0;
}
qDebug()<<"license serial number"<<license_data.serial_number;
/* check md5 value */
QByteArray ba_licdata((char *)&license_data,(sizeof(license_data) - sizeof(license_data.md5)));
//memcpy(ba_licdata.data(),(char *)&license_data,(sizeof(license_data) - sizeof(license_data.md5)));
QByteArray hash_data;
hash_data = QCryptographicHash::hash(ba_licdata, QCryptographicHash::Md5);
if(memcmp(&license_data.md5[0],hash_data.data(),LIC_DATA_MD5_LEN))
{
qDebug()<<"license file was damaged";
return 0;
}
else
{
return 1;
}
}
quint32 LicenseManger::CheckRemainingTime()
{
/* read data from file */
ReadLicDataFromFile();
/* check remaining time */
if(license_data.remainingTime >= 6*60*18*367)
{
qDebug()<<"license file was invalid";
return 0;
}
if(license_data.remainingTime)
license_data.remainingTime--;
if(license_data.remainingTime<=0)
{
license_data.workout = 1;
}
SaveLicData2File();
//license_data.remainingTime=0;
return license_data.remainingTime;
}
quint32 LicenseManger::CheckUpdateFileState()
{
QFile file(UPDATE_LIC_FILE);
LICENSE_DATA_t update_license_data;
quint32 state = UPDATE_LICENSE_FILE_OK;
if(!file.exists())
{
state = -UPDATE_LICENSE_FILE_NOT_EXIT;
}
else
{
if(!file.open(QIODevice::ReadOnly | QIODevice::Truncate)){
QMessageBox::warning(0,"警告",QString("无法打开license文件") );
state = -UPDATE_LICENSE_FILE_OPEN_FAILED;
}
else
{
//license_data_mutex.lock();
char encrypt_buff[1024];
char decrypt_buff[1024];
file.read((char *)encrypt_buff,(qint64)sizeof(encrypt_buff));
pAes->OnAesUncrypt((LPVOID)encrypt_buff, (DWORD)sizeof(encrypt_buff),(LPVOID)decrypt_buff); //进行解密
//qDebug()<<"jiemi:"<<decrypt_buff;
memcpy(&update_license_data,decrypt_buff,sizeof(update_license_data));
file.close();
//license_data_mutex.unlock();
/* check md5 value */
QByteArray ba_update_licdata;
memcpy(ba_update_licdata.data(),(char *)&update_license_data,(sizeof(update_license_data) - sizeof(update_license_data.md5)));
QByteArray hash_data;
hash_data = QCryptographicHash::hash(ba_update_licdata,QCryptographicHash::Md5 );
if(memcmp(&update_license_data.md5[0],hash_data.data(),LIC_DATA_MD5_LEN)) //file is invalid
{
state = -UPDATE_LICENSE_FILE_MD5_VALUE_ERR;
}
if( update_license_data.serial_number != machine_info.serial_number)
{
state = -UPDATE_LICENSE_FILE_SERIAL_NUMBER_ERR;
}
else if( update_license_data.printer_active_count <= license_data.printer_active_count ||
update_license_data.printer_active_count >= (license_data.printer_active_count+3) )
{
state = -UPDATE_LICENSE_FILE_ACTIVE_NUMBER_ERR;
}
else if(update_license_data.vernon_bach_version != VERNONBACH_VERSION_CODE)
{
state = -UPDATE_LICENSE_FILE_SW_VERSION_ERR;
}
}
}
return state;
}
quint32 LicenseManger::UpdateLicenseFileData()
{
QFile file(UPDATE_LIC_FILE);
LICENSE_DATA_t update_license_data;
quint32 state = UPDATE_LICENSE_FILE_OK;
if(!file.exists())
{
state = -UPDATE_LICENSE_FILE_NOT_EXIT;
}
else
{
if(!file.open(QIODevice::ReadOnly | QIODevice::Truncate)){
QMessageBox::warning(0,"警告",QString("无法打开更新license文件") );
state = -UPDATE_LICENSE_FILE_OPEN_FAILED;
}
else
{
//license_data_mutex.lock();
char encrypt_buff[1024];
char decrypt_buff[1024];
file.read((char *)encrypt_buff,(qint64)sizeof(encrypt_buff));
pAes->OnAesUncrypt((LPVOID)encrypt_buff, (DWORD)sizeof(encrypt_buff),(LPVOID)decrypt_buff); //进行解密
//qDebug()<<"jiemi:"<<decrypt_buff;
memcpy(&update_license_data,decrypt_buff,sizeof(update_license_data));
file.close();
//license_data_mutex.unlock();
memcpy(&license_data, &update_license_data, sizeof(update_license_data));
SaveLicData2File();
}
}
return state;
}