123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "SysInfoExt.h"
- #include <QProcess>
- #ifdef Q_OS_LINUX
- # include <QCryptographicHash>
- # include <cpuid.h>
- #endif
- namespace SysInfoExt {
- #ifdef Q_OS_LINUX
- struct CPUInfo
- {
- QString cpuID;
- QString devID;
- };
- QByteArray GetMD5Hash(QByteArray buff)
- {
- QCryptographicHash hash(QCryptographicHash::Md5);
- hash.addData(buff);
- return hash.result().toHex();
- }
- QString GetCpuId()
- {
- QString cpuid = "";
- unsigned int dwBuf[4];
- int InfoType = 1;
- __cpuid(InfoType, dwBuf[0], dwBuf[1], dwBuf[2], dwBuf[3]);
- __cpuid_count(InfoType, 0, dwBuf[0], dwBuf[1], dwBuf[2], dwBuf[3]);
- cpuid = QString::number(dwBuf[3], 16).toUpper();
- cpuid = cpuid + QString::number(dwBuf[0], 16).toUpper();
- return cpuid;
- }
- QString GetMeCode()
- {
- QString strMe = GetCpuId();
- QByteArray ba = strMe.toLatin1();
- QString strTemp = ba.toBase64();
- return strTemp;
- }
- CPUInfo getCPUInfo()
- {
- CPUInfo cpuinfo;
- QByteArray hwid = GetMeCode().toUtf8().toHex();
- QByteArray algo = {};
- uint8_t hwid_cnt = hwid.length() / 2;
- for (int i = 0; i < hwid_cnt; i++) {
- QByteArray sum0 = GetMD5Hash(hwid.mid(i * 2, 2)).mid(hwid_cnt, 2);
- QByteArray csum = GetMD5Hash(sum0).mid(i, 2).toUpper();
- algo.append(csum);
- }
- const QString pcuid = { QString("%0").arg(GetMD5Hash(algo).data()) };
- cpuinfo.cpuID.append(QString("%0").arg(hwid.data()).toUpper());
- cpuinfo.devID.append(QString("%0").arg(pcuid).toUpper());
- return cpuinfo;
- }
- #endif
- QString fixInfo(const QString &str)
- {
- QString temp = str;
- // \r \n 去除
- temp.remove("\r");
- temp.remove("\n");
- // 去除多余的空格
- QStringList list;
- for (auto s : temp.split(" ")) {
- if (!s.trimmed().isEmpty()) {
- list.append(s);
- }
- }
- return list.join(" ");
- }
- /**
- * @brief 通用信息获取
- * @param cmd
- * @return
- */
- QString getWMIC(const QString &cmd)
- {
- QProcess p;
- p.start(cmd);
- p.waitForFinished();
- QString res = QString::fromLocal8Bit(p.readAllStandardOutput());
- return fixInfo(res);
- }
- /**
- * 功能: 获取CPU的名称
- */
- QString getCpuName()
- {
- #ifdef Q_OS_WIN
- return getWMIC("wmic cpu get name");
- #endif
- #ifdef Q_OS_LINUX
- return getWMIC("grep -m 1 'model name' /proc/cpuinfo");
- #endif
- return "";
- }
- /**
- * 功能: 获取CPU的ID
- */
- QString getCpuId()
- {
- #ifdef Q_OS_WIN
- return getWMIC("wmic cpu get processorid");
- #endif
- return "";
- }
- /**
- * 功能: 查询主板序号
- */
- QString getBaseboardNum()
- {
- #ifdef Q_OS_WIN
- return getWMIC("wmic baseboard get serialnumber");
- #endif
- return "";
- }
- /**
- * 功能: 查询BIOS序号
- */
- QString getBiosNum()
- {
- #ifdef Q_OS_WIN
- return getWMIC("wmic bios get serialnumber");
- #endif
- return "";
- }
- /**
- * 功能: 查询硬盘序号
- */
- QString getDiskNum()
- {
- #ifdef Q_OS_WIN
- return getWMIC("wmic diskdrive where index=0 get serialnumber");
- #endif
- #ifdef Q_OS_LINUX
- return getWMIC("lsblk --nodeps -no serial /dev/sda | head -n 1");
- #endif
- return "";
- }
- } // namespace SysInfoExt
|