Qt—利用网卡信息和md5生成验证串

来源:互联网 发布:上海黄金交易所软件下载 编辑:程序博客网 时间:2024/05/16 14:31

一个最简单的验证串生成例子:

#include <QtCore/QCoreApplication>#include <QtCore/QCryptographicHash>#include <QtCore/QFile>#include <Windows.h>#include <IPHlpApi.h>#pragma comment(lib,"Iphlpapi.lib")#include <string>using namespace std;bool  getMAC(char* mac){PIP_ADAPTER_INFO pAdapterInfo;PIP_ADAPTER_INFO pAdapter = NULL;DWORD dwRetVal = 0;ULONG ulOutBufLen = 0;// 第一次调用GetAdapterInfo获取ulOutBufLen大小if (GetAdaptersInfo( NULL, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW){pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); }if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {pAdapter = pAdapterInfo;if (pAdapter) {mac[0] =pAdapter->Address[0];mac[1] =pAdapter->Address[1];mac[2] =pAdapter->Address[2];mac[3] =pAdapter->Address[3];mac[4] =pAdapter->Address[4];mac[5] =pAdapter->Address[5];}}else{return false;}return true;}bool getCheck(){QFile snFile("sn");if (!snFile.exists()){return false;}if(!snFile.open(QIODevice::ReadOnly)){return false;}QByteArray szSN = snFile.readAll();char mac[6] = {0};if (!getMAC(mac)){return false;}for (char i=0; i<6;i++){mac[i] = ~mac[i];mac[i] = mac[i]|i;}QByteArray data(mac,6);data = QCryptographicHash::hash(data,QCryptographicHash::Md5);QString sn ;sn.append(data.toHex());sn = sn.toUpper();return (szSN == sn)?true:false;}bool getFile(const char* fileName){QFile snFile(fileName);if(!snFile.open(QIODevice::WriteOnly)){printf("open file %s failed.\r\n", fileName);return false;}char mac[6] = {0};if (!getMAC(mac)){printf("get mac address failed.\r\n");return false;}for (char i=0; i<6;i++){mac[i] = ~mac[i];mac[i] = mac[i]|i;}QByteArray data(mac,6);data = QCryptographicHash::hash(data,QCryptographicHash::Md5);QString sn ;sn.append(data.toHex());sn = sn.toUpper();snFile.write(sn.toAscii());snFile.flush();snFile.close();return true;}