From df4dbac04c3911aea19b3c280d04c79447b4aa17 Mon Sep 17 00:00:00 2001 From: Misophist Date: Fri, 11 Aug 2023 05:46:47 -0500 Subject: [PATCH] util: random_string is now more entropic (#29312) * 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 old-commit-hash: 4cad0a035dd8c78e0c44dc346708f2166cccb6c7 --- common/util.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/util.cc b/common/util.cc index 55a8b1fb3e..2914f6bcbc 100644 --- a/common/util.cc +++ b/common/util.cc @@ -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 pick(0, sizeof(chrs) - 2); + std::uniform_int_distribution pick(0, chrs.length() - 1); std::string s; s.reserve(length); while (length--) {