47 #include <QMessageBox> 49 #include "keygenWidget.hh" 54 KeyGen::KeyGen(QString n, QString cHash, QString pHash, QString cpHash, QString prHash, QStringList mHashes, QString request)
65 QString KeyGen::computeSignature(
const bool _utf8 )
const {
68 ADD_SALT_PRE(saltPre);
70 ADD_SALT_POST(saltPost);
72 QString keyRequest = saltPre + name + coreHash + pluginHash + cpuHash
73 + productHash + macHashes.join(
"") + saltPost;
75 QString requestSigCheck;
79 QCryptographicHash::hash(keyRequest.toUtf8(),
80 QCryptographicHash::Sha1).toHex();
83 QCryptographicHash::hash(keyRequest.toLatin1(),
84 QCryptographicHash::Sha1).toHex();
86 return requestSigCheck;
89 KeyGen::ValidationResult KeyGen::isValid()
const 91 if (requestSig == computeSignature(
true))
95 else if(requestSig == computeSignature(
false))
102 QString KeyGen::Generate(QString expiryDate)
const 106 ADD_SALT_PRE(saltPre);
108 ADD_SALT_POST(saltPost);
110 KeyGen::ValidationResult valid = isValid();
116 QString license_ =
"";
119 license_ += expiryDate +
"\n";
120 license_ += name +
"\n";
121 license_ += coreHash +
"\n";
122 license_ += pluginHash +
"\n";
123 license_ += cpuHash +
"\n";
124 license_ += productHash +
"\n";
125 license_ += macHashes.join(
"\n") +
"\n";
127 QString licenseTmp = saltPre + expiryDate + name + coreHash + pluginHash + cpuHash + productHash + macHashes.join(
"") + saltPost;
130 licenseHash = QCryptographicHash::hash ( licenseTmp.toUtf8() , QCryptographicHash::Sha1 ).toHex();
132 licenseHash = QCryptographicHash::hash ( licenseTmp.toLatin1() , QCryptographicHash::Sha1 ).toHex();
134 license_ = licenseHash +
"\n" + license_;
139 QString KeyGen::filterString(QString in) {
140 const QRegExp validChar(
"[a-f0-9]");
141 QString out; out.reserve(in.size());
142 for (QString::iterator it = in.begin(), it_end = in.end(); it != it_end; ++it) {
143 if (validChar.exactMatch(*it))
149 std::vector<KeyGen> KeyGen::CreateFromMessyString(QString info)
151 const QString dirt =
"[\\s;>]*";
152 const QRegExp rx(
"\\b([\\w-]+)" + dirt +
"((?:(?:[a-f0-9]" + dirt +
"){40}){6,})\\b");
153 const QRegExp partRe(
"((?:[a-f0-9]" + dirt +
"){40})");
155 std::vector<KeyGen> R;
157 while ((pos = rx.indexIn(info, pos)) != -1) {
158 QString hashesStr = rx.cap(2);
161 while ((hashPos = partRe.indexIn(hashesStr, hashPos)) != -1) {
162 hashes.append(filterString(partRe.cap(1)));
163 hashPos += partRe.matchedLength();
167 std::copy(hashes.begin() + 4, hashes.end() - 1, std::back_inserter(macList));
175 hashes[hashes.count()-1]);
177 pos += rx.matchedLength();
183 KeyGenWidget::KeyGenWidget(QMainWindow *parent)
184 : QMainWindow(parent)
187 connect(generateAllButton,SIGNAL(clicked()),
this,SLOT(slotGenerateAllButton()));
188 connect(generateLocalButton,SIGNAL(clicked()),
this,SLOT(slotGenerateButton()));
189 connect(keyList->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(handleSelectionChanged(QItemSelection)));
191 connect(splitButton,SIGNAL(clicked()),
this,SLOT(slotSplit()));
193 connect(requestData,SIGNAL(textChanged()),
this,SLOT(slotAnalyze()));
196 connect(days ,SIGNAL(valueChanged(
int)),
this,SLOT(slotDate()));
197 connect(months,SIGNAL(valueChanged(
int)),
this,SLOT(slotDate()));
198 connect(years ,SIGNAL(valueChanged(
int)),
this,SLOT(slotDate()));
203 connect(mangle_pb, SIGNAL(clicked()),
this, SLOT(slotMangle()));
207 expires->setDate( QDate::currentDate());
209 generateLocalButton->setVisible(
false);
210 generateAllButton->setVisible(
false);
213 void KeyGenWidget::slotMangle() {
214 const QString hardwareHash_raw = hardwareHashDump_te->toPlainText();
215 const QString pluginHashes_raw = pluginHashDump_te->toPlainText();
217 const std::vector<KeyGen> hardwareKeygens = KeyGen::CreateFromMessyString(hardwareHash_raw);
218 if (hardwareKeygens.empty()) {
219 QMessageBox::critical(
this, tr(
"Unable to Mangle"), tr(
"No valid request found in hardware textbox."));
222 KeyGen hardwareKeygen = hardwareKeygens.front();
224 std::vector<KeyGen> pluginKeygens = KeyGen::CreateFromMessyString(pluginHashes_raw);
225 if (pluginKeygens.empty()) {
226 QMessageBox::critical(
this, tr(
"Unable to Mangle"), tr(
"No valid request found in plugins textbox."));
230 QString generatedRequest;
231 for (std::vector<KeyGen>::iterator it = pluginKeygens.begin(), it_end = pluginKeygens.end();
232 it != it_end; ++it) {
234 it->copyHardwareHashesFrom(hardwareKeygen);
236 generatedRequest += it->generateRequest();
239 requestData->setPlainText(generatedRequest);
242 void KeyGenWidget::slotDate() {
243 QDate today = QDate::currentDate();
244 today = today.addDays(days->value());
245 today = today.addMonths(months->value());
246 today = today.addYears(years->value());
248 expires->setDate(today);
251 void KeyGenWidget::slotAnalyze() {
252 QString inputData = requestData->toPlainText();
253 keygens_ = KeyGen::CreateFromMessyString(inputData);
256 for (std::vector<KeyGen>::const_iterator it = keygens_.begin(), it_end = keygens_.end();
257 it != it_end; ++it) {
258 QListWidgetItem *newItem =
new QListWidgetItem( keyList);
259 newItem->setText(it->name);
260 newItem->setHidden(
false);
261 KeyGen::ValidationResult r = it->isValid();
263 newItem->setTextColor(QColor(255, 0, 0));
264 else if (r == KeyGen::LATIN1)
265 newItem->setTextColor(QColor(128, 128, 0));
268 generateLocalButton->setVisible(
false);
269 generateAllButton->setVisible(keygens_.size());
272 void KeyGenWidget::slotSplit() {
274 QString inputData = requestData->toPlainText();
277 QStringList data = inputData.split(
";",QString::SkipEmptyParts);
279 QString newText = data.join(
"\n");
281 requestData->setText(newText);
285 void KeyGenWidget::handleSelectionChanged(
const QItemSelection& selection){
286 generateLocalButton->setVisible(
false);
287 if(keyList->selectionModel()->selectedIndexes().count())
289 int i = keyList->selectionModel()->selectedIndexes()[0].row();
290 setKeyGen(&keygens_[i]);
291 generateLocalButton->setVisible(
true);
292 generateAllButton->setVisible(
true);
294 KeyGen::ValidationResult valid = keygens_[i].isValid();
295 if (valid == KeyGen::INVALID)
296 lbWarning->setText(
"ERROR: Signature does not match.\nCannot generate key");
297 else if (valid == KeyGen::LATIN1)
298 lbWarning->setText(
"WARNING: Request uses old Ascii format.\nKey will be generated with Ascii encoding.");
300 lbWarning->setText(
"");
304 KeyGenWidget::~KeyGenWidget() {
308 void KeyGenWidget::toFile(
const KeyGen* gen)
310 QString licenseFileName_ = gen->name;
311 std::cerr <<
"Writing License file to output : " << licenseFileName_.toStdString() << std::endl;
312 QFile outFile(licenseFileName_ +
".lic");
314 if (!outFile.open(QIODevice::WriteOnly|QIODevice::Text)) {
315 QMessageBox::critical(
this,tr(
"Unable to open file"),tr(
"Unable to Open output File"));
319 QTextStream output(&outFile);
320 output << gen->Generate(expires->date().toString(Qt::ISODate));
324 void KeyGenWidget::setKeyGen(
const KeyGen* gen) {
325 fileNameBox->setText(gen->name);
326 coreHashBox->setText(gen->coreHash);
327 pluginHashBox->setText(gen->pluginHash);
328 cpuHashBox->setText(gen->cpuHash);
329 productIDBox->setText(gen->productHash);
330 macHashBox->setText(gen->macHashes.join(
"\n"));
331 signatureBox->setText(gen->requestSig);
332 generateLocalButton->setEnabled(gen->isValid());
335 void KeyGenWidget::slotGenerateButton() {
336 if(keyList->selectionModel()->selectedIndexes().count())
338 int i = keyList->selectionModel()->selectedIndexes()[0].row();
339 toFile(&keygens_[i]);
343 void KeyGenWidget::slotGenerateAllButton() {
344 for(
unsigned int i = 0; i < keygens_.size(); i++)
345 toFile(&keygens_[i]);