| 208 | } |
| 209 | |
| 210 | QByteArray ExternalMediaFileStorage::decrypt(QByteArray const& encryptedData, QByteArray const& key, QByteArray const& nonce) const { |
| 211 | QByteArray decryptedData(encryptedData.size() - cryptoGetHeaderSize(), '\0'); |
| 212 | unsigned long long decrypted_len; |
| 213 | if (crypto_aead_xchacha20poly1305_ietf_decrypt(reinterpret_cast<unsigned char*>(decryptedData.data()), &decrypted_len, NULL, reinterpret_cast<unsigned char const*>(encryptedData.data()), encryptedData.size(), NULL, 0, reinterpret_cast<unsigned char const*>(nonce.data()), reinterpret_cast<unsigned char const*>(key.data())) != 0) { |
| 214 | throw openmittsu::exceptions::InternalErrorException() << "Could not decrypt blob, data corrupt!"; |
| 215 | } else if (static_cast<int>(decrypted_len) != (encryptedData.size() - cryptoGetHeaderSize())) { |
| 216 | throw openmittsu::exceptions::InternalErrorException() << "Could not decrypt media item blob: Expected size was " << (encryptedData.size() - cryptoGetHeaderSize()) << " Bytes, but we got " << decrypted_len << " Bytes instead!"; |
| 217 | } |
| 218 | |
| 219 | return decryptedData; |
| 220 | } |
| 221 | |
| 222 | QByteArray ExternalMediaFileStorage::generateKey() const { |
| 223 | QByteArray keyBytes(cryptoGetKeySize(), '\0'); |