QByteArray PgpManager::SignPresence (const QByteArray& status) { QCA::SecureMessageKey msgKey; if (PrivateKey_.isNull ()) { warning (QString ("Cannot sign: private key is null")); return QByteArray(); } msgKey.setPGPSecretKey (PrivateKey_); QCA::OpenPGP pgp; QCA::SecureMessage msg (&pgp); msg.setFormat (QCA::SecureMessage::Ascii); msg.setSigner (msgKey); msg.startSign (QCA::SecureMessage::Detached); msg.update (status); msg.end (); msg.waitForFinished (); if (msg.success ()) return msg.signature (); else { info (QString { "Error signing: %1" } .arg (msg.errorCode ()) .arg (msg.diagnosticText ())); return QByteArray (); } }
QByteArray PgpManager::SignMessage (const QByteArray& body) { QCA::SecureMessageKey msgKey; if (PrivateKey_.isNull ()) { warning (QString ("Cannot sign: private key is null")); return QByteArray (); } msgKey.setPGPSecretKey (PrivateKey_); QCA::OpenPGP pgp; QCA::SecureMessage msg (&pgp); msg.setFormat (QCA::SecureMessage::Ascii); msg.setSigner (msgKey); msg.startSign (QCA::SecureMessage::Detached); msg.update (body); msg.end (); msg.waitForFinished (); if (!msg.success ()) { warning (QString { "Error signing: %1 (%2)." } .arg (msg.errorCode ()) .arg (msg.diagnosticText ())); return QByteArray (); } const auto& sig = msg.signature (); const auto& arrs = sig.split ('\n'); auto it = arrs.begin (); ++it; if (it == arrs.end ()) return sig; for (; it != arrs.end (); ++it) if (it->isEmpty ()) break; if (++it >= arrs.end ()) return sig; QByteArray result; for (; it != arrs.end (); ++it) { if (it->at (0) == '-') break; result += *it; result += '\n'; } result.chop (1); return result; }