Skip to content

Commit

Permalink
Replace use of std::rand with improved C++11 random
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Cambra <[email protected]>
  • Loading branch information
claucambra committed May 20, 2023
1 parent 9eeeeba commit ddd9290
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/gui/filedetails/sharemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QFileInfo>
#include <QTimeZone>

#include <random>
#include <openssl/rand.h>

#include "account.h"
Expand Down Expand Up @@ -54,6 +55,9 @@ QString createRandomPassword()
{ specialChars, specialCharMatch },
};

std::random_device rand_dev;
std::mt19937 rng(rand_dev());

QString passwd;
unsigned char unsignedCharArray[numChars];

Expand All @@ -76,8 +80,11 @@ QString createRandomPassword()
}

// add random required character at random position
const auto passwdInsertIndex = std::rand() % passwd.length();
const auto charToInsertIndex = std::rand() % selectionChars.length();
std::uniform_int_distribution<std::mt19937::result_type> passwdDist(0, passwd.length() - 1);
std::uniform_int_distribution<std::mt19937::result_type> charsDist(0, selectionChars.length() - 1);

const auto passwdInsertIndex = passwdDist(rng);
const auto charToInsertIndex = charsDist(rng);
const auto charToInsert = selectionChars.at(charToInsertIndex);

passwd.insert(passwdInsertIndex, charToInsert);
Expand Down

0 comments on commit ddd9290

Please sign in to comment.