Skip to content

Commit

Permalink
Use Qt instead of C string functions to search the hash
Browse files Browse the repository at this point in the history
  • Loading branch information
wolframroesler committed Mar 21, 2020
1 parent 3514b08 commit 6b8d02d
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/core/HibpDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

namespace
{

/*
* Return the SHA1 hash of the specified password in upper-case hex.
*
Expand Down Expand Up @@ -59,25 +58,34 @@ namespace
*/
int pwnCount(const QString& password, const QString& hibpResult)
{
const auto hash = sha1Hex(password).toStdString();
const auto result = hibpResult.toStdString();
const auto pHash = hash.c_str();
const auto pResult = result.c_str();

// The first 5 characters of the hash are in the URL. Search the
// rest in the HIBP result.
const auto p = strstr(pResult, pHash + 5);
if (p) {

// Found: Return the number after the next colon
const auto colon = strchr(p, ':');
return colon ? atoi(colon + 1) : 1;

} else {

// The first 5 characters of the hash are in the URL already,
// the HIBP result contains the remainder, which is:
const auto hash = sha1Hex(password);
const auto remainder = QStringRef(&hash, 5, 35);

// Search the remainder in the HIBP output
const auto pos = hibpResult.indexOf(remainder);
if (pos < 0) {
// Not found
return 0;
}

// Found: Return the number that follows. We know that the
// length of remainder is 35 and that a colon follows in
// the HIBP result, followed by the number. So the number
// begins here:
const auto counter = hibpResult.midRef(pos+35+1);

// And where does the number end?
auto end = counter.indexOf('\n');
if (end < 0) {
end = counter.size();
}

// So extract the number. Note that toInt doesn't have
// a "scan until number ends, ignore whatever follows"
// mode like atoi.
return counter.left(end).toInt();
}
} // namespace

Expand Down

0 comments on commit 6b8d02d

Please sign in to comment.