52 #if QT_VERSION >= 0x050000 58 #include <QMessageBox> 60 #include "keygenWidget.hh" 65 KeyGen::KeyGen(QString n, QString cHash, QString pHash, QString cpHash, QString prHash, QStringList mHashes, QString request)
76 QString KeyGen::computeSignature(
const bool _utf8 )
const {
79 ADD_SALT_PRE(saltPre);
81 ADD_SALT_POST(saltPost);
83 QString keyRequest = saltPre + name + coreHash + pluginHash + cpuHash
84 + productHash + macHashes.join(
"") + saltPost;
86 QString requestSigCheck;
90 QCryptographicHash::hash(keyRequest.toUtf8(),
91 QCryptographicHash::Sha1).toHex();
94 QCryptographicHash::hash(keyRequest.toLatin1(),
95 QCryptographicHash::Sha1).toHex();
97 return requestSigCheck;
100 KeyGen::ValidationResult KeyGen::isValid()
const 102 if (requestSig == computeSignature(
true))
106 else if(requestSig == computeSignature(
false))
113 QString KeyGen::Generate(QString expiryDate)
const 117 ADD_SALT_PRE(saltPre);
119 ADD_SALT_POST(saltPost);
121 KeyGen::ValidationResult valid = isValid();
127 QString license_ =
"";
130 license_ += expiryDate +
"\n";
131 license_ += name +
"\n";
132 license_ += coreHash +
"\n";
133 license_ += pluginHash +
"\n";
134 license_ += cpuHash +
"\n";
135 license_ += productHash +
"\n";
136 license_ += macHashes.join(
"\n") +
"\n";
138 QString licenseTmp = saltPre + expiryDate + name + coreHash + pluginHash + cpuHash + productHash + macHashes.join(
"") + saltPost;
141 licenseHash = QCryptographicHash::hash ( licenseTmp.toUtf8() , QCryptographicHash::Sha1 ).toHex();
143 licenseHash = QCryptographicHash::hash ( licenseTmp.toLatin1() , QCryptographicHash::Sha1 ).toHex();
145 license_ = licenseHash +
"\n" + license_;
150 QString KeyGen::filterString(QString in) {
151 const QRegExp validChar(
"[a-f0-9]");
152 QString out; out.reserve(in.size());
153 for (QString::iterator it = in.begin(), it_end = in.end(); it != it_end; ++it) {
154 if (validChar.exactMatch(*it))
160 std::vector<KeyGen> KeyGen::CreateFromMessyString(QString info)
162 const QString dirt =
"[\\s;>]*";
163 const QRegExp rx(
"\\b([\\w-]+)" + dirt +
"((?:(?:[a-f0-9]" + dirt +
"){40}){6,})\\b");
164 const QRegExp partRe(
"((?:[a-f0-9]" + dirt +
"){40})");
166 std::vector<KeyGen> R;
168 while ((pos = rx.indexIn(info, pos)) != -1) {
169 QString hashesStr = rx.cap(2);
172 while ((hashPos = partRe.indexIn(hashesStr, hashPos)) != -1) {
173 hashes.append(filterString(partRe.cap(1)));
174 hashPos += partRe.matchedLength();
178 std::copy(hashes.begin() + 4, hashes.end() - 1, std::back_inserter(macList));
186 hashes[hashes.count()-1]);
188 pos += rx.matchedLength();
194 KeyGenWidget::KeyGenWidget(QMainWindow *parent)
195 : QMainWindow(parent)
198 connect(generateAllButton,SIGNAL(clicked()),
this,SLOT(slotGenerateAllButton()));
199 connect(generateLocalButton,SIGNAL(clicked()),
this,SLOT(slotGenerateButton()));
200 connect(keyList->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(handleSelectionChanged(QItemSelection)));
202 connect(splitButton,SIGNAL(clicked()),
this,SLOT(slotSplit()));
204 connect(requestData,SIGNAL(textChanged()),
this,SLOT(slotAnalyze()));
207 connect(days ,SIGNAL(valueChanged(
int)),
this,SLOT(slotDate()));
208 connect(months,SIGNAL(valueChanged(
int)),
this,SLOT(slotDate()));
209 connect(years ,SIGNAL(valueChanged(
int)),
this,SLOT(slotDate()));
214 connect(mangle_pb, SIGNAL(clicked()),
this, SLOT(slotMangle()));
218 expires->setDate( QDate::currentDate());
220 generateLocalButton->setVisible(
false);
221 generateAllButton->setVisible(
false);
224 void KeyGenWidget::slotMangle() {
225 const QString hardwareHash_raw = hardwareHashDump_te->toPlainText();
226 const QString pluginHashes_raw = pluginHashDump_te->toPlainText();
228 const std::vector<KeyGen> hardwareKeygens = KeyGen::CreateFromMessyString(hardwareHash_raw);
229 if (hardwareKeygens.empty()) {
230 QMessageBox::critical(
this, tr(
"Unable to Mangle"), tr(
"No valid request found in hardware textbox."));
233 KeyGen hardwareKeygen = hardwareKeygens.front();
235 std::vector<KeyGen> pluginKeygens = KeyGen::CreateFromMessyString(pluginHashes_raw);
236 if (pluginKeygens.empty()) {
237 QMessageBox::critical(
this, tr(
"Unable to Mangle"), tr(
"No valid request found in plugins textbox."));
241 QString generatedRequest;
242 for (std::vector<KeyGen>::iterator it = pluginKeygens.begin(), it_end = pluginKeygens.end();
243 it != it_end; ++it) {
245 it->copyHardwareHashesFrom(hardwareKeygen);
247 generatedRequest += it->generateRequest();
250 requestData->setPlainText(generatedRequest);
253 void KeyGenWidget::slotDate() {
254 QDate today = QDate::currentDate();
255 today = today.addDays(days->value());
256 today = today.addMonths(months->value());
257 today = today.addYears(years->value());
259 expires->setDate(today);
262 void KeyGenWidget::slotAnalyze() {
263 QString inputData = requestData->toPlainText();
264 keygens_ = KeyGen::CreateFromMessyString(inputData);
267 for (std::vector<KeyGen>::const_iterator it = keygens_.begin(), it_end = keygens_.end();
268 it != it_end; ++it) {
269 QListWidgetItem *newItem =
new QListWidgetItem( keyList);
270 newItem->setText(it->name);
271 newItem->setHidden(
false);
272 KeyGen::ValidationResult r = it->isValid();
274 newItem->setTextColor(QColor(255, 0, 0));
275 else if (r == KeyGen::LATIN1)
276 newItem->setTextColor(QColor(128, 128, 0));
279 generateLocalButton->setVisible(
false);
280 generateAllButton->setVisible(keygens_.size());
283 void KeyGenWidget::slotSplit() {
285 QString inputData = requestData->toPlainText();
288 QStringList data = inputData.split(
";",QString::SkipEmptyParts);
290 QString newText = data.join(
"\n");
292 requestData->setText(newText);
296 void KeyGenWidget::handleSelectionChanged(
const QItemSelection& selection){
297 generateLocalButton->setVisible(
false);
298 if(keyList->selectionModel()->selectedIndexes().count())
300 int i = keyList->selectionModel()->selectedIndexes()[0].row();
301 setKeyGen(&keygens_[i]);
302 generateLocalButton->setVisible(
true);
303 generateAllButton->setVisible(
true);
305 KeyGen::ValidationResult valid = keygens_[i].isValid();
306 if (valid == KeyGen::INVALID)
307 lbWarning->setText(
"ERROR: Signature does not match.\nCannot generate key");
308 else if (valid == KeyGen::LATIN1)
309 lbWarning->setText(
"WARNING: Request uses old Ascii format.\nKey will be generated with Ascii encoding.");
311 lbWarning->setText(
"");
315 KeyGenWidget::~KeyGenWidget() {
319 void KeyGenWidget::toFile(
const KeyGen* gen)
321 QString licenseFileName_ = gen->name;
322 std::cerr <<
"Writing License file to output : " << licenseFileName_.toStdString() << std::endl;
323 QFile outFile(licenseFileName_ +
".lic");
325 if (!outFile.open(QIODevice::WriteOnly|QIODevice::Text)) {
326 QMessageBox::critical(
this,tr(
"Unable to open file"),tr(
"Unable to Open output File"));
330 QTextStream output(&outFile);
331 output << gen->Generate(expires->date().toString(Qt::ISODate));
335 void KeyGenWidget::setKeyGen(
const KeyGen* gen) {
336 fileNameBox->setText(gen->name);
337 coreHashBox->setText(gen->coreHash);
338 pluginHashBox->setText(gen->pluginHash);
339 cpuHashBox->setText(gen->cpuHash);
340 productIDBox->setText(gen->productHash);
341 macHashBox->setText(gen->macHashes.join(
"\n"));
342 signatureBox->setText(gen->requestSig);
343 generateLocalButton->setEnabled(gen->isValid());
346 void KeyGenWidget::slotGenerateButton() {
347 if(keyList->selectionModel()->selectedIndexes().count())
349 int i = keyList->selectionModel()->selectedIndexes()[0].row();
350 toFile(&keygens_[i]);
354 void KeyGenWidget::slotGenerateAllButton() {
355 for(
unsigned int i = 0; i < keygens_.size(); i++)
356 toFile(&keygens_[i]);
bool connect(const QString &name, const bool create)
Connect INIFile object with given filename.