<commit_before>/*
 * Skaffari - a mail account administration web interface based on Cutelyst
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _XOPEN_SOURCE
#include "password.h"
#include <unistd.h>
#include <QSqlQuery>
#include <QFile>
#include <Cutelyst/Plugins/Utils/Sql>
#include <QCryptographicHash>

Q_LOGGING_CATEGORY(SK_PASSWORD, "<PASSWORD>")

Password::Password(const QString &pw) :
    m_password(pw)
{

}


QByteArray Password::encrypt(Method method, Algorithm algo, quint32 rounds)
{
    QByteArray pw;

    if (method == PlainText) {

        pw = m_password.toUtf8();
        qCWarning(SK_PASSWORD) << "Do not used unencrypted passwords!";

    } else if (method == Crypt) {

        QByteArray settings;
        uint pwSize = 0;

        if (algo == CryptDES) {

            settings = Password::requestSalt(2, QByteArrayLiteral("./0123456789ABCDEFGHIJKLMNJOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
            pwSize = 13;
            qCWarning(SK_PASSWORD) << "Do not use weak hashing/encryption methods for passwords!";

        } else if (algo == CryptMD5) {

            settings = QByteArrayLiteral("$1$");
            settings.append(Password::requestSalt(8));
            settings.append(QByteArrayLiteral("$"));
            pwSize = (settings.size() + 22);
            qCWarning(SK_PASSWORD) << "Do not use weak hashing/encryption methods for passwords!";

        } else if ((algo == CryptSHA256) || (algo == CryptSHA512) || (algo == Default)) {

            if (algo == CryptSHA512) {
                settings = QByteArrayLiteral("$6$rounds=");
            } else {
                settings = QByteArrayLiteral("$5$rounds=");
            }

            if (rounds < 1000) {
                rounds = 1000;
            } else if (rounds > 999999999) {
                rounds = 999999999;
            }

            settings.append(QByteArray::number(rounds));
            settings.append(QByteArrayLiteral("$"));
            settings.append(Password::requestSalt(16, QByteArrayLiteral("./0123456789ABCDEFGHIJKLMNJOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")));
            settings.append(QByteArrayLiteral("$"));

            if (algo == CryptSHA512) {<commit_msg>Password: set default rounds to 5000 instead of bound limits<commit_after>/*
 * Skaffari - a mail account administration web interface based on Cutelyst
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _XOPEN_SOURCE
#include "password.h"
#include <unistd.h>
#include <QSqlQuery>
#include <QFile>
#include <Cutelyst/Plugins/Utils/Sql>
#include <QCryptographicHash>

Q_LOGGING_CATEGORY(SK_PASSWORD, "<PASSWORD>")

Password::Password(const QString &pw) :
    m_password(pw)
{

}


QByteArray Password::encrypt(Method method, Algorithm algo, quint32 rounds)
{
    QByteArray pw;

    if (method == PlainText) {

        pw = m_password.toUtf8();
        qCWarning(SK_PASSWORD) << "Do not use unencrypted passwords!";

    } else if (method == Crypt) {

        QByteArray settings;
        uint pwSize = 0;

        if (algo == CryptDES) {

            settings = Password::requestSalt(2, QByteArrayLiteral("./0123456789ABCDEFGHIJKLMNJOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
            pwSize = 13;
            qCWarning(SK_PASSWORD) << "Do not use weak hashing/encryption methods for passwords!";

        } else if (algo == CryptMD5) {

            settings = QByteArrayLiteral("$1$");
            settings.append(Password::requestSalt(8));
            settings.append(QByteArrayLiteral("$"));
            pwSize = (settings.size() + 22);
            qCWarning(SK_PASSWORD) << "Do not use weak hashing/encryption methods for passwords!";

        } else if ((algo == CryptSHA256) || (algo == CryptSHA512) || (algo == Default)) {

            if (algo == CryptSHA512) {
                settings = QByteArrayLiteral("$6$rounds=");
            } else {
                settings = QByteArrayLiteral("$5$rounds=");
            }

            if (rounds < 1000) {
                rounds = 5000;
            } else if (rounds > 999999999) {
                rounds = 5000;
            }

            settings.append(QByteArray::number(rounds));
            settings.append(QByteArrayLiteral("$"));
            settings.append(Password::requestSalt(16, QByteArrayLiteral("./0123456789ABCDEFGHIJKLMNJOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")));
            settings.append(QByteArrayLiteral("$"));

            if (algo == CryptSHA512) {
{'fraction_non_alphanumeric': 0.10887306824101406, 'fraction_numerical': 0.027261677374544192, 'mean_word_length': 2.881401617250674, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1634853', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1649, 'n_words': 487}