| 94 | } |
| 95 | |
| 96 | QByteArray Crypto::decrypt(const QByteArray &data) |
| 97 | { |
| 98 | if (static_cast<quint64>(data.size()) < crypto_aead_chacha20poly1305_IETF_NPUBBYTES) |
| 99 | throw std::runtime_error(tr("Cipher text too short.").toUtf8().toStdString()); |
| 100 | QByteArray plainText(data.size() - crypto_aead_chacha20poly1305_IETF_ABYTES, 0); |
| 101 | quint64 plainTextLen; |
| 102 | QByteArray nonce = data.left(crypto_aead_chacha20poly1305_IETF_NPUBBYTES); |
| 103 | QByteArray cipherText = data.mid(crypto_aead_chacha20poly1305_IETF_NPUBBYTES); |
| 104 | if (crypto_aead_chacha20poly1305_ietf_decrypt(reinterpret_cast<unsigned char *>(plainText.data()), &plainTextLen, |
| 105 | nullptr, |
| 106 | reinterpret_cast<const unsigned char *>(cipherText.data()), cipherText.size(), |
| 107 | nullptr, 0, |
| 108 | reinterpret_cast<const unsigned char *>(nonce.data()), |
| 109 | reinterpret_cast<const unsigned char *>(sessionKey.data())) != 0) |
| 110 | throw std::runtime_error(tr("Decryption failed.").toUtf8().toStdString()); |
| 111 | return plainText.left(plainTextLen); |
| 112 | } |
| 113 | |
| 114 | void Crypto::init() |
| 115 | { |