dev/qt/ PgenPasswordGeneratorConsoleSource


Back to PgenPasswordGenerator

#include <QCoreApplication>
#include <QCryptographicHash>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // hard coded inputs 
    QString secret{"boing"};
    QString inp{"snarf"};

    // hashes
    QCryptographicHash sha(QCryptographicHash::Sha256);
    QCryptographicHash md5(QCryptographicHash::Md5);

    // generate inputs for the hashes
    QString sha_input = inp + secret;
    QString md5_input = QString("MrFlibble") + secret + "\n";

    // put data into the hashes
    sha.addData(sha_input.toUtf8());
    md5.addData(md5_input.toUtf8());

    // get output
    QByteArray sha_output = sha.result();
    QByteArray md5_output = md5.result();

    // the md5 version takes the hex output and sticks that through base64
    // this was a shortcoming of my first attempt at this, but since I use
    // the outputs to verify I've typed the secret correctly, this version
    // is retained.
    QByteArray md5_hex = md5_output.toHex();
    QByteArray md5_base64 = md5_hex.toBase64();
    QByteArray sha_base64 = sha_output.toBase64();

    // convert to base64
    QString md5_b64_str(md5_base64);
    QString sha_b64_str(sha_base64);

    // change /,+ to #,@ respectively
    QString md5_result_full = md5_b64_str.replace("/","#").replace("+","@");
    QString sha_result_full = sha_b64_str.replace("/","#").replace("+","@");

    // trim the strings to length
    QString md5_result = md5_result_full.left(16);
    QString sha_result = sha_result_full.left(16);

    // write the output
    QTextStream out(stdout);
    out << md5_result << "\n";
    out << sha_result << "\n";

    return 0;
}