[CSE] Generate a random name for the temporary file

This commit is contained in:
Tomaz Canabrava 2017-12-20 23:09:28 +01:00
parent 7e83f0591b
commit dd0528037d
3 changed files with 21 additions and 4 deletions

View file

@ -65,7 +65,21 @@ namespace {
}
}
QByteArray EncryptionHelper::generateRandom(int size) {
QByteArray EncryptionHelper::generateRandomString(int size)
{
const QByteArray possibleCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
QByteArray randomString(size, '\0');
for(int i=0; i < size; ++i)
{
int index = qrand() % possibleCharacters.length();
randomString[i] = possibleCharacters.at(index);
}
return randomString;
}
QByteArray EncryptionHelper::generateRandom(int size)
{
unsigned char *tmp = (unsigned char *)malloc(sizeof(unsigned char) * size);
int ret = RAND_bytes(tmp, size);

View file

@ -28,6 +28,7 @@ QString baseUrl();
class EncryptionHelper {
public:
static QByteArray generateRandomString(int size);
static QByteArray generateRandom(int size);
static QByteArray generatePassword(const QString &wordlist, const QByteArray& salt);
static QByteArray encryptPrivateKey(

View file

@ -126,7 +126,7 @@ void PropagateUploadEncrypted::slotFolderEncriptedMetadataReceived(const QJsonDo
qDebug() << "Creating the encrypted file metadata helper.";
EncryptedFile encryptedFile;
encryptedFile.authenticationTag = "NOISE"; // TODO: Remove the noise.
encryptedFile.encryptedFilename = EncryptionHelper::generateRandom(20);
encryptedFile.encryptedFilename = EncryptionHelper::generateRandomString(20);
encryptedFile.encryptionKey = EncryptionHelper::generateRandom(16);
encryptedFile.fileVersion = 1;
encryptedFile.initializationVector = EncryptionHelper::generateRandom(16);
@ -135,10 +135,12 @@ void PropagateUploadEncrypted::slotFolderEncriptedMetadataReceived(const QJsonDo
metaData.addEncryptedFile(encryptedFile);
qDebug() << "Encrypting the file";
QFile *input = new QFile(info.absoluteFilePath());
auto *input = new QFile(info.absoluteFilePath());
//TODO: Perhaps I should use a QTemporaryFile?
QFile *output = new QFile(QDir::tempPath() + encryptedFile.encryptedFilename);
qDebug() << "Creating " << QDir::tempPath() + QDir::separator() + encryptedFile.encryptedFilename;
auto *output = new QFile(QDir::tempPath() + QDir::separator() + encryptedFile.encryptedFilename);
EncryptionHelper::fileEncryption(encryptedFile.encryptionKey,
encryptedFile.initializationVector,