Kirigami-addons

passwordhealth.cpp
1// SPDX-FileCopyrightText: 2025 Carl Schwan <carl@carlschwan.eu>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#include "passwordhealth.h"
5#include "zxcvbn/zxcvbn.h"
6
7constexpr static int ZXCVBN_ESTIMATE_THRESHOLD = 256;
8
9PasswordHealth::PasswordHealth(QObject *parent)
10 : QObject(parent)
11{}
12
13QString PasswordHealth::password() const
14{
15 return m_password;
16}
17
18void PasswordHealth::setPassword(const QString &password)
19{
20 if (m_password == password) {
21 return;
22 }
23 m_password = password;
24
25 auto entropy = 0.0;
26 entropy += ZxcvbnMatch(m_password.left(ZXCVBN_ESTIMATE_THRESHOLD).toUtf8().data(), nullptr, nullptr);
27 if (m_password.length() > ZXCVBN_ESTIMATE_THRESHOLD) {
28 // Add the average entropy per character for any characters above the estimate threshold
29 auto average = entropy / ZXCVBN_ESTIMATE_THRESHOLD;
30 entropy += average * (m_password.length() - ZXCVBN_ESTIMATE_THRESHOLD);
31 }
32 m_entropy = entropy;
33
34 Q_EMIT passwordChanged();
35}
36
37double PasswordHealth::entropy() const
38{
39 return m_entropy;
40}
41
42PasswordHealth::Quality PasswordHealth::quality() const
43{
44 if (m_entropy <= 0) {
45 return Quality::Bad;
46 } else if (m_entropy < 40) {
47 return Quality::Poor;
48 } else if (m_entropy < 75) {
49 return Quality::Weak;
50 } else if (m_entropy < 100) {
51 return Quality::Good;
52 }
53 return Quality::Excellent;
54}
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 11:51:42 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.