Merge pull request #339 from ctrlaltca/qt5_no_libgcrypt
Drop libgcrypt dependency for qt5
This commit is contained in:
commit
283bac0b80
2 changed files with 46 additions and 24 deletions
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
PROJECT(servatrice)
|
PROJECT(servatrice)
|
||||||
|
|
||||||
FIND_PACKAGE(Libgcrypt REQUIRED)
|
|
||||||
|
|
||||||
SET(servatrice_SOURCES
|
SET(servatrice_SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/passwordhasher.cpp
|
src/passwordhasher.cpp
|
||||||
|
@ -29,6 +27,10 @@ if(Qt4_FOUND)
|
||||||
INCLUDE(${QT_USE_FILE})
|
INCLUDE(${QT_USE_FILE})
|
||||||
include_directories(${QT_INCLUDES})
|
include_directories(${QT_INCLUDES})
|
||||||
LIST(APPEND SERVATRICE_LIBS ${QT_LIBRARIES})
|
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()
|
endif()
|
||||||
|
|
||||||
# qt5 stuff
|
# qt5 stuff
|
||||||
|
@ -60,7 +62,6 @@ SET(QT_DONT_USE_QTGUI TRUE)
|
||||||
|
|
||||||
# Include directories
|
# Include directories
|
||||||
INCLUDE_DIRECTORIES(../common)
|
INCLUDE_DIRECTORIES(../common)
|
||||||
INCLUDE_DIRECTORIES(${LIBGCRYPT_INCLUDE_DIR})
|
|
||||||
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
|
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
|
||||||
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../common)
|
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../common)
|
||||||
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
|
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|
|
@ -1,32 +1,53 @@
|
||||||
#include "passwordhasher.h"
|
#include "passwordhasher.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
#if QT_VERSION < 0x050000
|
||||||
#include <gcrypt.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <gcrypt.h>
|
||||||
|
#else
|
||||||
|
#include <QCryptographicHash>
|
||||||
|
#endif
|
||||||
|
|
||||||
void PasswordHasher::initialize()
|
void PasswordHasher::initialize()
|
||||||
{
|
{
|
||||||
// These calls are required by libgcrypt before we use any of its functions.
|
#if QT_VERSION < 0x050000
|
||||||
gcry_check_version(0);
|
// These calls are required by libgcrypt before we use any of its functions.
|
||||||
gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
|
gcry_check_version(0);
|
||||||
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 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)
|
QString PasswordHasher::computeHash(const QString &password, const QString &salt)
|
||||||
{
|
{
|
||||||
const int algo = GCRY_MD_SHA512;
|
const int algo = GCRY_MD_SHA512;
|
||||||
const int rounds = 1000;
|
const int rounds = 1000;
|
||||||
|
|
||||||
QByteArray passwordBuffer = (salt + password).toUtf8();
|
QByteArray passwordBuffer = (salt + password).toUtf8();
|
||||||
int hashLen = gcry_md_get_algo_dlen(algo);
|
int hashLen = gcry_md_get_algo_dlen(algo);
|
||||||
char *hash = new char[hashLen], *tmp = new char[hashLen];
|
char *hash = new char[hashLen], *tmp = new char[hashLen];
|
||||||
gcry_md_hash_buffer(algo, hash, passwordBuffer.data(), passwordBuffer.size());
|
gcry_md_hash_buffer(algo, hash, passwordBuffer.data(), passwordBuffer.size());
|
||||||
for (int i = 1; i < rounds; ++i) {
|
for (int i = 1; i < rounds; ++i) {
|
||||||
memcpy(tmp, hash, hashLen);
|
memcpy(tmp, hash, hashLen);
|
||||||
gcry_md_hash_buffer(algo, hash, tmp, hashLen);
|
gcry_md_hash_buffer(algo, hash, tmp, hashLen);
|
||||||
}
|
}
|
||||||
QString hashedPass = salt + QString(QByteArray(hash, hashLen).toBase64());
|
QString hashedPass = salt + QString(QByteArray(hash, hashLen).toBase64());
|
||||||
delete[] tmp;
|
delete[] tmp;
|
||||||
delete[] hash;
|
delete[] hash;
|
||||||
return hashedPass;
|
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
|
||||||
|
|
Loading…
Reference in a new issue