From f48aedd89fc7654a8435367aa50a124724dfcd0c Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Wed, 14 Sep 2022 20:16:59 +0200 Subject: BitsOfBinaryData: Add fromByteArray() utility function It automatically hashes the data and creates a content ID. --- src/base/QXmppBitsOfBinaryData.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/base/QXmppBitsOfBinaryData.cpp') diff --git a/src/base/QXmppBitsOfBinaryData.cpp b/src/base/QXmppBitsOfBinaryData.cpp index 1592a2e5..6f9226de 100644 --- a/src/base/QXmppBitsOfBinaryData.cpp +++ b/src/base/QXmppBitsOfBinaryData.cpp @@ -40,6 +40,29 @@ QXmppBitsOfBinaryDataPrivate::QXmppBitsOfBinaryDataPrivate() /// \since QXmpp 1.2 /// +/// +/// Creates bits of binary data from a QByteArray. +/// +/// This hashes the data to generate a content ID. The MIME type is not set. +/// +/// \note This blocks while hashing the data. You may want to run this via QtConcurrent or a +/// QThreadPool to run this on for large amounts of data. +/// +/// \since QXmpp 1.5 +/// +QXmppBitsOfBinaryData QXmppBitsOfBinaryData::fromByteArray(QByteArray data) +{ + QXmppBitsOfBinaryContentId cid; + cid.setHash(QCryptographicHash::hash(data, QCryptographicHash::Sha1)); + cid.setAlgorithm(QCryptographicHash::Sha1); + + QXmppBitsOfBinaryData bobData; + bobData.d->cid = std::move(cid); + bobData.d->data = std::move(data); + + return bobData; +} + /// /// Default constructor /// -- cgit v1.2.3