Add crypto classes and tests. Link to libgcrypt.
This commit is contained in:
80
src/crypto/Crypto.cpp
Normal file
80
src/crypto/Crypto.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Crypto.h"
|
||||
|
||||
#include <QtCore/QMutex>
|
||||
|
||||
#include <gcrypt.h>
|
||||
|
||||
bool Crypto::m_initiated(false);
|
||||
|
||||
int gcry_qt_mutex_init(void** p_sys)
|
||||
{
|
||||
*p_sys = new QMutex();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gcry_qt_mutex_destroy(void** p_sys)
|
||||
{
|
||||
delete reinterpret_cast<QMutex*>(*p_sys);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gcry_qt_mutex_lock(void** p_sys)
|
||||
{
|
||||
reinterpret_cast<QMutex*>(*p_sys)->lock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gcry_qt_mutex_unlock(void** p_sys)
|
||||
{
|
||||
reinterpret_cast<QMutex*>(*p_sys)->unlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct gcry_thread_cbs gcry_threads_qt =
|
||||
{
|
||||
GCRY_THREAD_OPTION_USER,
|
||||
NULL,
|
||||
gcry_qt_mutex_init,
|
||||
gcry_qt_mutex_destroy,
|
||||
gcry_qt_mutex_lock,
|
||||
gcry_qt_mutex_unlock
|
||||
};
|
||||
|
||||
Crypto::Crypto()
|
||||
{
|
||||
}
|
||||
|
||||
void Crypto::init()
|
||||
{
|
||||
if (m_initiated) {
|
||||
return;
|
||||
}
|
||||
|
||||
gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_qt);
|
||||
gcry_check_version(0);
|
||||
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
|
||||
|
||||
m_initiated = true;
|
||||
}
|
||||
|
||||
bool Crypto::selfTest()
|
||||
{
|
||||
return (gcry_control(GCRYCTL_SELFTEST) == 0);
|
||||
}
|
||||
Reference in New Issue
Block a user