diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2018-04-03 12:40:53 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2018-04-10 12:17:43 +0000 |
commit | 5e7a64d1e321ddba751c0b18daf7920fde7c7de0 (patch) | |
tree | 8fc42dd4023135e99e7de90f39dfa8527db11926 /src/corelib | |
parent | e027c7241f073f5fc8c1db88700cc83635b6454f (diff) |
QCryptographicHash: Add a static method to retrieve hash length
It's useful when you need to check how long a hash will be without first
generating one.
[ChangeLog][QtCore][QCryptographicHash] Add a static method, hashLength,
which returns the length of the output of a hash function in bytes.
Change-Id: Id6a454016523de83d157fd95c50105c6db4bb1d9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qcryptographichash.cpp | 40 | ||||
-rw-r--r-- | src/corelib/tools/qcryptographichash.h | 1 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index a1b121f1ee..3c79bb797d 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -544,6 +544,46 @@ QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method) return hash.result(); } +/*! + Returns the size of the output of the selected hash \a method in bytes. + + \since 5.12 +*/ +int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method) +{ + switch (method) { + case QCryptographicHash::Sha1: + return 20; +#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 + case QCryptographicHash::Md4: + return 16; + case QCryptographicHash::Md5: + return 16; + case QCryptographicHash::Sha224: + return SHA224HashSize; + case QCryptographicHash::Sha256: + return SHA256HashSize; + case QCryptographicHash::Sha384: + return SHA384HashSize; + case QCryptographicHash::Sha512: + return SHA512HashSize; + case QCryptographicHash::RealSha3_224: + case QCryptographicHash::Keccak_224: + return 224 / 8; + case QCryptographicHash::RealSha3_256: + case QCryptographicHash::Keccak_256: + return 256 / 8; + case QCryptographicHash::RealSha3_384: + case QCryptographicHash::Keccak_384: + return 384 / 8; + case QCryptographicHash::RealSha3_512: + case QCryptographicHash::Keccak_512: + return 512 / 8; +#endif + } + return 0; +} + QT_END_NAMESPACE #ifndef QT_NO_QOBJECT diff --git a/src/corelib/tools/qcryptographichash.h b/src/corelib/tools/qcryptographichash.h index 2f74d42405..ad1de7c756 100644 --- a/src/corelib/tools/qcryptographichash.h +++ b/src/corelib/tools/qcryptographichash.h @@ -101,6 +101,7 @@ public: QByteArray result() const; static QByteArray hash(const QByteArray &data, Algorithm method); + static int hashLength(Algorithm method); private: Q_DISABLE_COPY(QCryptographicHash) QCryptographicHashPrivate *d; |