Skip to content

Commit f30ce1c

Browse files
committed
Write generated hash to a file.
1 parent 23c46f0 commit f30ce1c

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

license-check-console.pro.user

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.2.1, 2017-02-19T03:10:13. -->
3+
<!-- Written by QtCreator 4.2.1, 2017-02-19T16:19:10. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

license.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
#include "license.h"
22

3+
#include <string>
4+
#include <iostream>
5+
6+
#include <QFile>
7+
#include <QDebug>
8+
#include <QTextStream>
39
#include <QCryptographicHash>
410

11+
void License::printLicenseHash() const
12+
{
13+
std::string str = _licenseHash.toStdString();
14+
std::cout << "[ " << str << " ]";
15+
16+
qDebug() << QString::fromStdString(str);
17+
}
18+
519
QByteArray License::generateLicenseHash(QByteArray addr)
620
{
721
return QCryptographicHash::hash(addr, QCryptographicHash::Md4);
822
}
923

24+
void License::saveToFile(QString fileName, QString path)
25+
{
26+
QFile file(fileName);
27+
QDir::setCurrent(path);
28+
29+
if ( !file.open(QIODevice::WriteOnly | QIODevice::Text) ) {
30+
qDebug() << "Failed to open file.";
31+
return;
32+
}
33+
34+
QTextStream stream(&file);
35+
foreach(char c, _licenseHash) {
36+
stream << c;
37+
}
38+
}
39+
1040
QDate License::getExpiryDate() const
1141
{
1242
return _expiryDate;

license.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
#ifndef LICENSE_H
22
#define LICENSE_H
33

4-
#include <QHash>
4+
#include <QDir>
55
#include <QDate>
6-
//#include <QFile>
6+
#include <QString>
77
#include <QByteArray>
88

99
class License
1010
{
1111
public:
1212
// Constructors
1313
License() :
14-
_macAddress(""), _expiryDate(QDate::currentDate()), _licenseHash("") {}
14+
_macAddress(""), _expiryDate(QDate::currentDate()), _licenseHash("")
15+
{
16+
saveToFile(QString("test.txt"));
17+
}
1518

1619
License(QString mac):
1720
_macAddress(mac), _expiryDate(QDate::currentDate())
1821
{
19-
_licenseHash = generateLicenseHash(_macAddress.toLatin1());
22+
_licenseHash = generateLicenseHash(_macAddress.toUtf8());
23+
saveToFile(QString("test.txt"));
2024
}
2125

2226
License(QString mac, QDate expiry) :
2327
_macAddress(mac),
2428
_expiryDate(expiry)
2529
{
26-
_licenseHash = generateLicenseHash(_macAddress.toLatin1());
30+
_licenseHash = generateLicenseHash(_macAddress.toUtf8());
31+
saveToFile(QString("test.txt"));
2732
}
2833

34+
// Print the generated license hash
35+
void printLicenseHash() const;
36+
2937
// Generate the Hash from a MAC addr
3038
QByteArray generateLicenseHash(QByteArray addr);
3139

3240
// Write the generated license hash and expiry date to a file
33-
//void saveToFile();
41+
void saveToFile(QString fileName, QString path=QDir::homePath());
3442

3543
// Getter methods
3644
QDate getExpiryDate() const;

main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int main(int argc, char *argv[])
2222
Validate v(l);
2323

2424
v.isValidLicense() ? qDebug() << "Valid License" : qDebug() << "Invalid License" ;
25+
l->printLicenseHash();
2526

2627
return a.exec();
2728
}

0 commit comments

Comments
 (0)