keygenWidget.cc 2.89 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3

#include <QtGui>
#include "keygenWidget.hh"
Jan Möbius's avatar
Jan Möbius committed
4
#include <iostream>
Jan Möbius's avatar
Jan Möbius committed
5

Jan Möbius's avatar
Jan Möbius committed
6
7
8
9
#include "salt.hh"

KeyGenWidget::KeyGenWidget(QMainWindow *parent)
    : QMainWindow(parent)
Jan Möbius's avatar
Jan Möbius committed
10
{
Jan Möbius's avatar
Jan Möbius committed
11
12
  setupUi(this);
  connect(generateButton,SIGNAL(clicked()),this,SLOT(slotGenerateButton()));
Jan Möbius's avatar
Jan Möbius committed
13
14
15
16
17
  
  // Automatically set expire date to current date + 1 Year
  QDate today = QDate::currentDate();
  expires->setDate(today.addYears(1));
  
Jan Möbius's avatar
Jan Möbius committed
18
}
Jan Möbius's avatar
Jan Möbius committed
19

Jan Möbius's avatar
Jan Möbius committed
20
KeyGenWidget::~KeyGenWidget() {
Jan Möbius's avatar
Jan Möbius committed
21
22
23

}

Jan Möbius's avatar
Jan Möbius committed
24
25
void KeyGenWidget::slotGenerateButton() {

Jan Möbius's avatar
Jan Möbius committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  QString inputData = requestData->toPlainText();
  QStringList data = inputData.split('\n',QString::SkipEmptyParts);

  if ( data.size() != 5) {
    QMessageBox::critical(this,"Wrong request data","The request has to contain 5 lines of data");
  } else {

    // Clean strings
    QString name       = data[0].simplified();
    QString coreHash   = data[1].simplified();
    QString pluginHash = data[2].simplified();
    QString macHash    = data[3].simplified();
    QString requestSig = data[4].simplified();

    QString expiryDate = expires->date().toString(Qt::ISODate);

    std::cerr << "Generating key for Plugin : " << name.toStdString()       << std::endl;
    std::cerr << "Core Hash                 : " << coreHash.toStdString()   << std::endl;
    std::cerr << "Plugin Hash               : " << pluginHash.toStdString() << std::endl;
    std::cerr << "macHash is                : " << macHash.toStdString()    << std::endl;
    std::cerr << "requestSignature is       : " << requestSig.toStdString() << std::endl;
    std::cerr << "expiryDate is             : " << expiryDate.toStdString() << std::endl;

    // Get the salts
    QString saltPre;
    ADD_SALT_PRE(saltPre);
    QString saltPost;
    ADD_SALT_POST(saltPost);
Jan Möbius's avatar
Jan Möbius committed
54

Jan Möbius's avatar
Jan Möbius committed
55
56
57
58
59
60
    QString keyRequest = saltPre + name + coreHash + pluginHash + macHash + saltPost;
    QString requestSigCheck = QCryptographicHash::hash ( keyRequest.toAscii()  , QCryptographicHash::Sha1 ).toHex();
    
    if ( requestSig != requestSigCheck ) {
      QMessageBox::critical(this,"Signature of request invalid","The signature of the request is not valid");
      return;
Jan Möbius's avatar
Jan Möbius committed
61
    }
Jan Möbius's avatar
Jan Möbius committed
62

Jan Möbius's avatar
Jan Möbius committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    std::cerr << "Writing License file to output : " << name.toStdString() << std::endl;
    QFile outFile(name + ".lic");

    if (!outFile.open(QIODevice::WriteOnly|QIODevice::Text)) {
      QMessageBox::critical(this,"Unable to open file","Unable to Open output File");
      return;
    }

    QTextStream output(&outFile);

    // Add basic hashes
    output << name         << "\n";
    output << coreHash     << "\n";
    output << pluginHash   << "\n";
    output << macHash      << "\n";
    output << expiryDate << "\n";
    
    // Sign the license file
    QString license = saltPre + name + coreHash + pluginHash + macHash + expiryDate + saltPost;
    QString licenseHash = QCryptographicHash::hash ( license.toAscii()  , QCryptographicHash::Sha1 ).toHex();
    
    output << licenseHash;

    outFile.close();
  }
  
  close();

Jan Möbius's avatar
Jan Möbius committed
91
92
  
 
Jan Möbius's avatar
Jan Möbius committed
93
}