diff --git a/servatrice/CMakeLists.txt b/servatrice/CMakeLists.txt index 42e2bb74..925e9611 100644 --- a/servatrice/CMakeLists.txt +++ b/servatrice/CMakeLists.txt @@ -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}) diff --git a/servatrice/src/passwordhasher.cpp b/servatrice/src/passwordhasher.cpp index 0d8cda36..554c8f2f 100644 --- a/servatrice/src/passwordhasher.cpp +++ b/servatrice/src/passwordhasher.cpp @@ -1,32 +1,53 @@ #include "passwordhasher.h" -#include -#include -#include + +#if QT_VERSION < 0x050000 + #include + #include + #include +#else + #include +#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