The code below shows how to calculate an md5crypt based password. This code is compatible with the glibc code.
#include <QCoreApplication>
#include <QtCrypto>
#include <QtDebug>
#include <cstdio>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
QString to64(long v, int size)
{
QString itoa64 = QStringLiteral("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
QString result;
while (--size >= 0) {
result.append(itoa64.at((int)(v & 0x3f)));
v = v >> 6;
}
return result;
}
int byte2unsigned(int byteValue)
{
int integerToReturn;
integerToReturn = (int)byteValue & 0xff;
return integerToReturn;
}
{
finalState = hash2.
final();
for (
int i = password.
size(); i > 0; i -= 16) {
}
for (
int i = password.
size(); i != 0; i = i >> 1) {
if ((i & 1) != 0) {
} else {
}
}
finalState = hash1.
final();
for (int i = 0; i < 1000; i++) {
if ((i & 1) != 0) {
} else {
}
if ((i % 3) != 0) {
}
if ((i % 7) != 0) {
}
if ((i & 1) != 0) {
} else {
}
finalState = hash2.
final();
}
QString encodedString;
encodedString.append(QString::fromLatin1(magic_string.
toByteArray()));
encodedString.append(QString::fromLatin1(salt.
toByteArray()));
encodedString.append(QStringLiteral("$"));
long l;
l = (byte2unsigned(finalState.
toByteArray().at(0)) << 16 | (byte2unsigned(finalState.
toByteArray().at(6))) << 8 |
encodedString.append(to64(l, 4));
l = (byte2unsigned(finalState.
toByteArray().at(1)) << 16 | (byte2unsigned(finalState.
toByteArray().at(7))) << 8 |
encodedString.append(to64(l, 4));
l = (byte2unsigned(finalState.
toByteArray().at(2)) << 16 | (byte2unsigned(finalState.
toByteArray().at(8))) << 8 |
encodedString.append(to64(l, 4));
l = (byte2unsigned(finalState.
toByteArray().at(3)) << 16 | (byte2unsigned(finalState.
toByteArray().at(9))) << 8 |
encodedString.append(to64(l, 4));
l = (byte2unsigned(finalState.
toByteArray().at(4)) << 16 | (byte2unsigned(finalState.
toByteArray().at(10))) << 8 |
encodedString.append(to64(l, 4));
encodedString.append(to64(l, 2));
return encodedString;
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
if (argc < 3) {
printf("Usage: %s password salt (salt without $1$)\n", argv[0]);
return 1;
}
printf("MD5 hash not supported!\n");
else {
QString result = qca_md5crypt(password, salt);
printf(
"md5crypt [ %s , %s ] = '%s'\n", password.
data(), salt.
data(), qPrintable(result));
}
return 0;
}