-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #412 from deXol/develop402IntegrateHIBP
#402 Integrate hibp
- Loading branch information
Showing
9 changed files
with
251 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "HaveIBeenPwned.h" | ||
|
||
#include <QNetworkReply> | ||
#include <QCryptographicHash> | ||
|
||
HaveIBeenPwned::HaveIBeenPwned(QObject *parent) : | ||
QObject(parent), | ||
networkManager(new QNetworkAccessManager(this)) | ||
{ | ||
QObject::connect(networkManager, &QNetworkAccessManager::finished, this, &HaveIBeenPwned::processReply); | ||
} | ||
|
||
/** | ||
* @brief HaveIBeenPwned::isPasswordPwned | ||
* @param pwd Given password to check | ||
* @param formatString Formatting the response | ||
* Calculating the SHA1 hash of the password | ||
* and sending the first five char to HIBP v2 API. | ||
*/ | ||
void HaveIBeenPwned::isPasswordPwned(const QString &pwd, const QString &formatString) | ||
{ | ||
QCryptographicHash sha1Hasher(QCryptographicHash::Sha1); | ||
sha1Hasher.addData(pwd.toUtf8()); | ||
hash = sha1Hasher.result().toHex().toUpper(); | ||
this->formatString = formatString; | ||
req.setUrl(QUrl(HIBP_API + hash.left(HIBP_REQUEST_SHA_LENGTH))); | ||
hash = hash.mid(HIBP_REQUEST_SHA_LENGTH); | ||
networkManager->get(req); | ||
} | ||
|
||
/** | ||
* @brief HaveIBeenPwned::processReply | ||
* @param reply HIBP password check request reply | ||
* Processing the answer of the password HIBP request. | ||
*/ | ||
void HaveIBeenPwned::processReply(QNetworkReply *reply) | ||
{ | ||
if (reply->error()) | ||
{ | ||
qDebug() << reply->errorString(); | ||
return; | ||
} | ||
|
||
QString answer = reply->readAll(); | ||
|
||
/** | ||
* Checking if the answer contains the remaining of | ||
* the password hash, and getting the pwned number. | ||
*/ | ||
if (answer.contains(hash)) | ||
{ | ||
QString fromPwned = answer.mid(answer.indexOf(hash)); | ||
QString pwned = fromPwned.left(fromPwned.indexOf(HASH_SEPARATOR)); | ||
QString pwnedNum = pwned.mid(pwned.indexOf(':') + 1); | ||
emit sendPwnedMessage(formatString.arg(pwnedNum)); | ||
} | ||
else | ||
{ | ||
emit safePassword(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef HAVEIBEENPWNED_H | ||
#define HAVEIBEENPWNED_H | ||
|
||
#include <QObject> | ||
#include <QNetworkAccessManager> | ||
#include <QNetworkRequest> | ||
|
||
class HaveIBeenPwned : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit HaveIBeenPwned(QObject *parent = nullptr); | ||
|
||
void isPasswordPwned(const QString &pwd, const QString &formatString); | ||
|
||
signals: | ||
/** | ||
* @brief sendPwnedNum | ||
* @param message | ||
* Sending signal how many times the given password | ||
* has been compromised | ||
*/ | ||
void sendPwnedMessage(QString message); | ||
|
||
/** | ||
* @brief safePassword | ||
* Sending signal if the given password is safe | ||
*/ | ||
void safePassword(); | ||
|
||
public slots: | ||
void processReply(QNetworkReply *reply); | ||
|
||
private: | ||
QNetworkAccessManager *networkManager = nullptr; | ||
QNetworkRequest req; | ||
|
||
QString hash; | ||
QString formatString; | ||
|
||
const QString HIBP_API = "https://api.pwnedpasswords.com/range/"; | ||
const QString HASH_SEPARATOR = "\r\n"; | ||
const int HIBP_REQUEST_SHA_LENGTH = 5; | ||
}; | ||
|
||
#endif // HAVEIBEENPWNED_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.