Skip to content

Commit

Permalink
util: random_string is now more entropic (commaai#29312)
Browse files Browse the repository at this point in the history
* random_string now returns a significantly higher entropy random string

sizeof(char*) is going to be 8 on a 64-bit system, not the length of the
string. Before, only strings of integer values [0-6] were being generated. Also
switched to C++ constructs for simplicity.

* Update common/util.cc

* maintain repo style

---------

Co-authored-by: Shane Smiskol <shane@smiskol.com>
  • Loading branch information
2 people authored and Edison-CBS committed Aug 11, 2023
1 parent 8af79fc commit ba72f54
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions common/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ std::string hexdump(const uint8_t* in, const size_t size) {
}

std::string random_string(std::string::size_type length) {
const char* chrs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string chrs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::mt19937 rg{std::random_device{}()};
std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
std::uniform_int_distribution<std::string::size_type> pick(0, chrs.length() - 1);
std::string s;
s.reserve(length);
while (length--) {
Expand Down

0 comments on commit ba72f54

Please sign in to comment.