Drop libgcrypt dependency for qt5

This commit is contained in:
Fabio Bas 2014-10-01 20:21:22 +02:00
parent 7eed007f14
commit 355de8fba4
2 changed files with 46 additions and 24 deletions

View file

@ -4,8 +4,6 @@
PROJECT(servatrice)
FIND_PACKAGE(Libgcrypt REQUIRED)
SET(servatrice_SOURCES
src/main.cpp
src/passwordhasher.cpp
@ -29,6 +27,10 @@ if(Qt4_FOUND)
INCLUDE(${QT_USE_FILE})
include_directories(${QT_INCLUDES})
LIST(APPEND SERVATRICE_LIBS ${QT_LIBRARIES})
# Libgcrypt is required only with Qt4 to support SHA512 hashing
FIND_PACKAGE(Libgcrypt REQUIRED)
INCLUDE_DIRECTORIES(${LIBGCRYPT_INCLUDE_DIR})
endif()
# qt5 stuff
@ -60,7 +62,6 @@ SET(QT_DONT_USE_QTGUI TRUE)
# Include directories
INCLUDE_DIRECTORIES(../common)
INCLUDE_DIRECTORIES(${LIBGCRYPT_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../common)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

View file

@ -1,32 +1,53 @@
#include "passwordhasher.h"
#include <stdio.h>
#include <string.h>
#include <gcrypt.h>
#if QT_VERSION < 0x050000
#include <stdio.h>
#include <string.h>
#include <gcrypt.h>
#else
#include <QCryptographicHash>
#endif
void PasswordHasher::initialize()
{
// These calls are required by libgcrypt before we use any of its functions.
gcry_check_version(0);
gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
#if QT_VERSION < 0x050000
// These calls are required by libgcrypt before we use any of its functions.
gcry_check_version(0);
gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
#endif
}
#if QT_VERSION < 0x050000
QString PasswordHasher::computeHash(const QString &password, const QString &salt)
{
const int algo = GCRY_MD_SHA512;
const int rounds = 1000;
const int algo = GCRY_MD_SHA512;
const int rounds = 1000;
QByteArray passwordBuffer = (salt + password).toUtf8();
int hashLen = gcry_md_get_algo_dlen(algo);
char *hash = new char[hashLen], *tmp = new char[hashLen];
gcry_md_hash_buffer(algo, hash, passwordBuffer.data(), passwordBuffer.size());
for (int i = 1; i < rounds; ++i) {
memcpy(tmp, hash, hashLen);
gcry_md_hash_buffer(algo, hash, tmp, hashLen);
}
QString hashedPass = salt + QString(QByteArray(hash, hashLen).toBase64());
delete[] tmp;
delete[] hash;
return hashedPass;
QByteArray passwordBuffer = (salt + password).toUtf8();
int hashLen = gcry_md_get_algo_dlen(algo);
char *hash = new char[hashLen], *tmp = new char[hashLen];
gcry_md_hash_buffer(algo, hash, passwordBuffer.data(), passwordBuffer.size());
for (int i = 1; i < rounds; ++i) {
memcpy(tmp, hash, hashLen);
gcry_md_hash_buffer(algo, hash, tmp, hashLen);
}
QString hashedPass = salt + QString(QByteArray(hash, hashLen).toBase64());
delete[] tmp;
delete[] hash;
return hashedPass;
}
#else
QString PasswordHasher::computeHash(const QString &password, const QString &salt)
{
QCryptographicHash::Algorithm algo = QCryptographicHash::Sha512;
const int rounds = 1000;
QByteArray hash = (salt + password).toUtf8();
for (int i = 0; i < rounds; ++i) {
hash = QCryptographicHash::hash(hash, algo);
}
QString hashedPass = salt + QString(hash.toBase64());
return hashedPass;
}
#endif