This commit is contained in:
VanNostrand 2014-03-11 18:50:48 +00:00
commit 5f926cffd5

View file

@ -3,6 +3,8 @@
#include <string.h> #include <string.h>
#include <gcrypt.h> #include <gcrypt.h>
#define HASH_ALGO GCRY_MD_SHA512
void PasswordHasher::initialize() void PasswordHasher::initialize()
{ {
// These calls are required by libgcrypt before we use any of its functions. // These calls are required by libgcrypt before we use any of its functions.
@ -13,17 +15,16 @@ void PasswordHasher::initialize()
QString PasswordHasher::computeHash(const QString &password, const QString &salt) QString PasswordHasher::computeHash(const QString &password, const QString &salt)
{ {
const int algo = GCRY_MD_SHA512; // concatenate salt and password
const int rounds = 1000; const QByteArray passwordBuffer = (salt + password).toAscii();
QByteArray passwordBuffer = (salt + password).toAscii(); // create an array for the digest in the proper size for the chosen algorithm
int hashLen = gcry_md_get_algo_dlen(algo); char digest[gcry_md_get_algo_dlen(HASH_ALGO)];
char hash[hashLen], tmp[hashLen];
gcry_md_hash_buffer(algo, hash, passwordBuffer.data(), passwordBuffer.size()); // calculate the message digest of passwordBuffer
for (int i = 1; i < rounds; ++i) { gcry_md_hash_buffer(HASH_ALGO, digest, passwordBuffer.data(), passwordBuffer.size());
memcpy(tmp, hash, hashLen);
gcry_md_hash_buffer(algo, hash, tmp, hashLen); // concatenate and return salt and digest
} return salt + QString(QByteArray(digest).toBase64());
return salt + QString(QByteArray(hash, hashLen).toBase64());
} }