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;
}
{
hash1.update(password);
hash1.update(magic_string);
hash1.update(salt);
hash2.update(password);
hash2.update(salt);
hash2.update(password);
finalState = hash2.final();
for (
int i = password.
size(); i > 0; i -= 16) {
hash1.update(finalState.
toByteArray().left(i > 16 ? 16 : i));
}
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) {
hash2.update(password);
} else {
}
if ((i % 3) != 0) {
hash2.update(salt);
}
if ((i % 7) != 0) {
hash2.update(password);
}
if ((i & 1) != 0) {
} else {
hash2.update(password);
}
finalState = hash2.final();
}
QString encodedString;
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;
}
General class for hashing algorithms.
Definition: qca_basic.h:209
Convenience method for initialising and cleaning up QCA.
Definition: qca_core.h:660
Secure array of bytes.
Definition: qca_tools.h:317
int size() const
Returns the number of bytes in the array.
char * data()
Pointer to the data in the secure array.
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
void fill(char fillChar, int fillToPosition=-1)
Fill the data array with a specified character.
SecureArray & append(const SecureArray &a)
Append a secure byte array to the end of this array.
void clear()
Clears the contents of the array and makes it empty.
QCA_EXPORT void init()
Initialise QCA.
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.