diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-06-10 12:52:06 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-06-10 12:52:06 +0200 |
| commit | f6586ae894673b561455de5044d2ff31f6a0ef94 (patch) | |
| tree | 854df037b2f8b5c034bdb0b794c2e19ee47f9fe7 /src/crypt/cryptengine.cpp | |
| parent | 74bdec2ff94eec373eb358c00437d55fe44c9238 (diff) | |
| download | IT-Sicherheit-f6586ae894673b561455de5044d2ff31f6a0ef94.tar.gz IT-Sicherheit-f6586ae894673b561455de5044d2ff31f6a0ef94.zip | |
Diffstat (limited to 'src/crypt/cryptengine.cpp')
| -rw-r--r-- | src/crypt/cryptengine.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/crypt/cryptengine.cpp b/src/crypt/cryptengine.cpp index fcee6f2..905fa7d 100644 --- a/src/crypt/cryptengine.cpp +++ b/src/crypt/cryptengine.cpp @@ -18,6 +18,67 @@ void CryptEngine::generateRandomKey() m_cryptClass->generateRandomKey(); } +QByteArray CryptEngine::createPasswordHash(const QString &password) +{ + QByteArray ret(256, 0); + + EVP_MD_CTX *ctx; + + /* Create and initialise the context */ + if (!(ctx = EVP_MD_CTX_create())) + { + handleOpenSslError(); + } + + /* Initialise the encryption operation. IMPORTANT - ensure you use a key + * and IV size appropriate for your cipher */ + if (1 != EVP_DigestInit_ex(ctx, EVP_sha256(), NULL)) + { + handleOpenSslError(); + } + + /* Provide the message to be encrypted, and obtain the encrypted output. + * EVP_EncryptUpdate can be called multiple times if necessary + */ + if (1 != EVP_DigestUpdate(ctx, password.data(), password.size())) + { + handleOpenSslError(); + } + + /* Finalise the encryption. Further ciphertext bytes may be written at + * this stage. + */ + if (1 != EVP_DigestFinal_ex(ctx, (unsigned char *) ret.data(), NULL)) + { + handleOpenSslError(); + } + + EVP_MD_CTX_destroy(ctx); + + return ret; +} + +QByteArray CryptEngine::createRandomIv(int length) +{ + QByteArray ret(length, 0); + + qint64 m_time = QDateTime::currentMSecsSinceEpoch(); + + RAND_seed(&m_time, sizeof m_time); + + if(1 != RAND_bytes((unsigned char *) ret.data(), ret.length())) + { + handleOpenSslError(); + } + + return ret; +} + +void CryptEngine::handleOpenSslError() +{ + throw std::runtime_error(ERR_reason_error_string(ERR_get_error())); +} + void CryptEngine::setKey(QString value) { m_cryptClass->setKey( value.toLatin1() ); |
