Index: sources/view/settings/VSettings.cpp =================================================================== diff -u -r09a43d162e61ae02c73364e050d90957d736f322 -r4696b5fa37cc2ee744582fc70228736cad55ca63 --- sources/view/settings/VSettings.cpp (.../VSettings.cpp) (revision 09a43d162e61ae02c73364e050d90957d736f322) +++ sources/view/settings/VSettings.cpp (.../VSettings.cpp) (revision 4696b5fa37cc2ee744582fc70228736cad55ca63) @@ -38,7 +38,10 @@ void VSettings::servicePass_post(const QString &vservicePass) { Storage::Settings settings; - settings.save(servicePassCategory(), servicePassGroup(), servicePassKey(), vservicePass); + if ( settings.save(servicePassCategory(), servicePassGroup(), servicePassKey(), vservicePass) != 0 ) { + servicePass(""); + // FIXME: Notify UI with a message + } } void VSettings::alarmVolume_post(const quint8 &valarmVolume) { @@ -210,32 +213,65 @@ } /*! - * \brief VSettings::isPasswordConforming - * \details Validates the passed string for conforming to the "at least one - * capital letter, at least one digit, at least one symbol" rule + * \brief VSettings::isPasswordValid + * \details Validates the passed string for conforming to the + * - at least one capital letter, + * - at least one digit, + * - at least one symbol + * rule * \param vPassword - the string to check * \returns true if string pass conforms, false otherwise */ -bool VSettings::isPasswordConforming(const QString vPassword) { - QRegularExpression passwordStringRegexObject; +bool View::VSettings::isPasswordValid(const QString &vPassword) { + int minLen = 8 ; + QString pla = "(?=.*[%1])" ; // positive look ahead + QString regSntnc = "^%1$" ; // regular expression sentence with has a start/end + QString regUpper = "A-Z" ; + QString regLower = "a-z" ; + QString regDigit = "0-9" ; + QString regSymbl = "@$!%*?&.,_-" ; + QString rln = "[%1]{%2,}" ; - // Check for at least one capitalized letter - passwordStringRegexObject.setPattern("[A-Z]{1,}"); - bool hasCapitalized = passwordStringRegexObject.match(vPassword).hasMatch(); + // "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&])[A-Za-z0-9@$!%*?&]{8,}$" + QString regStr = regSntnc.arg( + pla.arg(regUpper) + + pla.arg(regLower) + + pla.arg(regDigit) + + pla.arg(regSymbl) + + rln.arg(regUpper + + regLower + + regDigit + + regSymbl) + .arg(minLen ) + ); + QRegularExpression passwordRegex(regStr); + bool ok; + ok = passwordRegex.match(vPassword).hasMatch(); + return ok; +} - // Check for at least one digit - passwordStringRegexObject.setPattern("[0-9]{1,}"); - bool hasNumeric = passwordStringRegexObject.match(vPassword).hasMatch(); +#include - // Check for at least one special character, defining it as a non-alphanumeric character - passwordStringRegexObject.setPattern("[^A-Za-z0-9]{1,}"); - bool hasSymbol = passwordStringRegexObject.match(vPassword).hasMatch(); - - return hasCapitalized && hasNumeric && hasSymbol; +QString VSettings::encryptString(const QString &vString) { + // FIXME: Move this to the utility, storage or settings controller class for more general use. + QString salt = "DVT-HD0004"; // FIXME: Use the actual HD serial number + int iter = 1000; + int len = 16; + auto shuffle = [](const QString &vString) { + // FIXME: Implement this, and make this a function and move this to the utility or storage class for more general use. + QString shuffled = vString; + return shuffled.toUtf8().toHex(); + }; + QCryptographicHash::Algorithm algorithm = QCryptographicHash::Sha512; + QByteArray hashed = QPasswordDigestor::deriveKeyPbkdf2(algorithm, vString.toUtf8(), shuffle(salt), iter, len); + return hashed.toHex(); } -// TODO this is a mock, need to add real encrypted string -QString VSettings::encryptString(const QString vString) { - return vString; +bool View::VSettings::isPasswordMatch(const QString &vPassword) { + return _servicePass == encryptString(vPassword); } +void View::VSettings::updatePassword(const QString &vPassword) +{ + servicePass(encryptString(vPassword)); +}