#include "encryption.h" // Linux // Qt #include #include // Project //#include "DeviceController.h" QString encryption::__salt__ = ""; // Additional int encryption::cryptOpen(const QString vDevice, const QString vFolder) { qDebug() << vDevice << vFolder; return 0; } int encryption::cryptFormat(const QString vDevice) { qDebug() << vDevice; return 0; } int encryption::cryptClose(const QString vFolder) { qDebug() << vFolder; return 0; } int encryption::mkfsExt4(const QString vDevice) { qDebug() << vDevice; return 0; } void encryption::varSalt(const QString &vSalt) { __salt__ = vSalt; } QString encryption::fixSalt() { const char c [ ] = { 56, 51, 104, 114, 48, 117, 50, 78, 51, 109, 94, 43, 105, 80, 48, 117, 114, 0 }; return QString(c); } /*! * \brief encryption::hashedString * \details encrypts the string vString with an algorithm * \param vString - the string to be encrypted. * \param vSalt - the encryption salt. * \return encrypted string */ QString encryption::hashedString(const QString &vString, bool &ok, bool vAddMoreSalt) { int iter = 1000; int len = 16; ok = vAddMoreSalt ? ! __salt__.isEmpty() : true; auto shuffle = [](const QString &vString) -> QByteArray { auto reverse = [](const QString &vString) -> QString { if ( vString.trimmed().isEmpty() ) return QString(); QString mReverse; int end = vString.size() - 1; int mid = end / 2; for(int i = mid; i >= 0; i-- ) { mReverse += vString.at(i); } for(int i = end; i > mid; i-- ) { mReverse += vString.at(i); } return mReverse; }; if ( vString.trimmed().isEmpty() ) return QByteArray(); //DEBUG: qDebug()<<"string" << vString; QString reversed = reverse(vString); //DEBUG: qDebug()<<"reversed" << reversed; QByteArray hexed = reversed.toUtf8().toHex(); //DEBUG: qDebug()<<"hexed" << hexed; return hexed; }; QCryptographicHash::Algorithm algorithm = QCryptographicHash::Sha512; QString salt = fixSalt() + (vAddMoreSalt ? __salt__ : ""); //DEBUG: qDebug() << "salt:" << salt; QByteArray hashed = QPasswordDigestor::deriveKeyPbkdf2(algorithm, vString.toUtf8(), shuffle( salt ), iter, len).toHex(); //DEBUG: qDebug() << "hashed:" << vString << hashed; return hashed; } /*! * \brief encryption::isDefaultServicePassword * \details Checks if the current service password is the default password * \note It is being used to force the user/manufacturer to set the service password. * \param vPassword - the service pasword entered by user. * \return true if the service password is the default. */ bool encryption::isDefaultServicePassword(const QString &vPassword, bool &ok) { //DEBUG qDebug() << __FUNCTION__ << vPassword; return vPassword == defaultServicePassword(ok); } /*! * \brief encryption::defaultPassword * \details The default password which will be used as an indication that the service password has not been set yet. * \return the default password as encrypted string. */ QString encryption::defaultServicePassword(bool &ok) { QString _A1 = "tal"; QString _S1 = QString("%1").arg(_A1 ).prepend("A" ); QString _S2 = QString("%1").arg(_A1 ).prepend("Ma" ); QString _S3 = QString("%1").arg(10*2+2 ).append ("leh" ); QString hashed = hashedString(( QStringList() << _S1 << _S2 << _S3 ).join('.'), ok, false); //DEBUG qDebug() << __FUNCTION__ << hashed; return hashed; }