Add reset() and blockSize().

This commit is contained in:
Felix Geyer 2010-09-13 23:18:31 +02:00
parent 26de957a98
commit bb6ae3a014
3 changed files with 35 additions and 3 deletions

View File

@ -43,7 +43,8 @@ CryptoHash::CryptoHash(CryptoHash::Algorithm algo)
break;
}
gcry_md_open(&d->ctx, algoGcrypt, 0); // TODO error handling
gcry_error_t error = gcry_md_open(&d->ctx, algoGcrypt, 0);
Q_ASSERT(error == 0); // TODO error handling
d->hashLen = gcry_md_get_algo_dlen(algoGcrypt);
}

View File

@ -25,6 +25,8 @@ public:
gcry_cipher_hd_t ctx;
SymmetricCipher::Direction direction;
QByteArray key;
QByteArray iv;
int blockSize;
};
SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
@ -35,6 +37,7 @@ SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCiphe
d->direction = direction;
d->key = key;
d->iv = iv;
int algoGcrypt;
@ -68,10 +71,15 @@ SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCiphe
error = gcry_cipher_open(&d->ctx, algoGcrypt, modeGcrypt, 0);
Q_ASSERT(error == 0); // TODO real error checking
error = gcry_cipher_setkey(d->ctx, d->key.constData(), d->key.size()); // TODO is key copied to gcrypt data structure?
error = gcry_cipher_setkey(d->ctx, d->key.constData(), d->key.size());
Q_ASSERT(error == 0);
error = gcry_cipher_setiv(d->ctx, iv.constData(), iv.size());
error = gcry_cipher_setiv(d->ctx, d->iv.constData(), d->iv.size());
Q_ASSERT(error == 0);
size_t blockSizeT;
error = gcry_cipher_algo_info(algoGcrypt, GCRYCTL_GET_BLKLEN, 0, &blockSizeT);
Q_ASSERT(error == 0);
d->blockSize = blockSizeT;
}
SymmetricCipher::~SymmetricCipher()
@ -123,3 +131,22 @@ void SymmetricCipher::processInPlace(QByteArray& data)
Q_ASSERT(error == 0);
}
void SymmetricCipher::reset()
{
Q_D(SymmetricCipher);
gcry_error_t error;
error = gcry_cipher_reset(d->ctx);
Q_ASSERT(error == 0);
error = gcry_cipher_setiv(d->ctx, d->iv.constData(), d->iv.size());
Q_ASSERT(error == 0);
}
int SymmetricCipher::blockSize() const
{
Q_D(const SymmetricCipher);
return d->blockSize;
}

View File

@ -45,9 +45,13 @@ public:
SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv);
~SymmetricCipher();
QByteArray process(const QByteArray& data);
void processInPlace(QByteArray& data);
void reset();
int blockSize() const;
private:
SymmetricCipherPrivate* const d_ptr;
Q_DECLARE_PRIVATE(SymmetricCipher);