summaryrefslogtreecommitdiff
path: root/svl
diff options
context:
space:
mode:
authorSarper Akdemir <sarper.akdemir.extern@allotropia.de>2023-11-08 07:36:52 +0300
committerSarper Akdemir <sarper.akdemir.extern@allotropia.de>2023-11-15 19:48:38 +0100
commitcdcff8c34144e883eca9dc6e1a85968ed34909c2 (patch)
tree3e976e02a3b3b43aa7962850b07efad63f5f25b1 /svl
parent51d9c9899f54200a2bf9bf49df16c03cca403498 (diff)
tdf#157518: add password strength meter to setmasterpassworddlg
Moves PasswordStrength bits that provide utility functions from cui to svl, merging them with PasswordHelper there. Adds password strength bar for the set master password dialog. (accessible via Tools -> Options -> LibreOffice -> Security -> Passwords for Web Connections) Change-Id: I8dc1090a041f8388c2e139beb1d0d9a0beb8acb0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159370 Tested-by: Jenkins Reviewed-by: Sarper Akdemir <sarper.akdemir.extern@allotropia.de>
Diffstat (limited to 'svl')
-rw-r--r--svl/Library_svl.mk2
-rw-r--r--svl/source/misc/PasswordHelper.cxx48
2 files changed, 50 insertions, 0 deletions
diff --git a/svl/Library_svl.mk b/svl/Library_svl.mk
index aee540d56514..56923d712ced 100644
--- a/svl/Library_svl.mk
+++ b/svl/Library_svl.mk
@@ -25,9 +25,11 @@ $(eval $(call gb_Library_use_externals,svl,\
curl) \
dtoa \
icu_headers \
+ icui18n \
icuuc \
mdds_headers \
libxml2 \
+ zxcvbn-c \
))
$(eval $(call gb_Library_set_componentfile,svl,svl/util/svl,services))
diff --git a/svl/source/misc/PasswordHelper.cxx b/svl/source/misc/PasswordHelper.cxx
index 41d7bf1ea68b..cfae72f64937 100644
--- a/svl/source/misc/PasswordHelper.cxx
+++ b/svl/source/misc/PasswordHelper.cxx
@@ -22,6 +22,11 @@
#include <comphelper/hash.hxx>
#include <rtl/digest.h>
#include <memory>
+#include <unicode/regex.h>
+#include <unicode/unistr.h>
+#include <unicode/errorcode.h>
+#include <zxcvbn.h>
+#include <sal/log.hxx>
using namespace com::sun::star;
@@ -129,4 +134,47 @@ bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPa
return bResult;
}
+double SvPasswordHelper::GetPasswordStrengthPercentage(const char* pPassword)
+{
+ // Entropy bits corresponding to 100% password strength
+ static constexpr double fMaxPassStrengthEntorpyBits = 112.0;
+ return std::min(100.0,
+ ZxcvbnMatch(pPassword, nullptr, nullptr) * 100.0 / fMaxPassStrengthEntorpyBits);
+}
+
+double SvPasswordHelper::GetPasswordStrengthPercentage(const OUString& aPassword)
+{
+ OString aPasswordUtf8 = aPassword.toUtf8();
+ return GetPasswordStrengthPercentage(aPasswordUtf8.getStr());
+}
+
+bool SvPasswordHelper::PasswordMeetsPolicy(const char* pPassword,
+ const std::optional<OUString>& oPasswordPolicy)
+{
+ if (oPasswordPolicy)
+ {
+ icu::ErrorCode aStatus;
+ icu::UnicodeString sPassword(pPassword);
+ icu::UnicodeString sRegex(oPasswordPolicy->getStr());
+ icu::RegexMatcher aRegexMatcher(sRegex, sPassword, 0, aStatus);
+
+ if (aRegexMatcher.matches(aStatus))
+ return true;
+
+ SAL_WARN_IF(
+ aStatus.isFailure(), "svl.misc",
+ "Password policy regular expression failed with error: " << aStatus.errorName());
+
+ return false;
+ }
+ return true;
+}
+
+bool SvPasswordHelper::PasswordMeetsPolicy(const OUString& aPassword,
+ const std::optional<OUString>& oPasswordPolicy)
+{
+ OString aPasswordUtf8 = aPassword.toUtf8();
+ return PasswordMeetsPolicy(aPasswordUtf8.getStr(), oPasswordPolicy);
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */