From 5ff5a32df70c001caf930a3cdfabea3f8bf0c24d Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 12 Jun 2020 18:41:42 +0200 Subject: [PATCH 1/2] Proper naming for RsRandom string functions --- libretroshare/src/util/rsrandom.cc | 25 +++++++++++++++++++------ libretroshare/src/util/rsrandom.h | 12 +++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/libretroshare/src/util/rsrandom.cc b/libretroshare/src/util/rsrandom.cc index 7ea8eb968..dc7d9ac67 100644 --- a/libretroshare/src/util/rsrandom.cc +++ b/libretroshare/src/util/rsrandom.cc @@ -121,13 +121,26 @@ double RsRandom::random_f64() return random_u64() / (double)(~(uint64_t)0) ; } -std::string RsRandom::random_alphaNumericString(uint32_t len) +/*static*/ std::string RsRandom::alphaNumeric(uint32_t length) { - std::string s = "" ; + std::string s; + while(s.size() < length) + { + uint8_t rChar; random_bytes(&rChar, 1); rChar = rChar % 123; + /* if(isalnum(val)) isalnum result may vary depend on locale!! */ + if( (rChar >= 48 && rChar <= 57) /* 0-9 */ || + (rChar >= 65 && rChar <= 90) /* A-Z */ || + (rChar >= 97 && rChar <= 122) /* a-z */ ) + s += static_cast(rChar); + } - for(uint32_t i=0;i(&ret[0]), length); + for(uint32_t i=0; i Date: Sat, 13 Jun 2020 14:58:30 +0200 Subject: [PATCH 2/2] Document what chars are return RsRandom::alphaNumeric As requested by Cyril --- libretroshare/src/util/rsrandom.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretroshare/src/util/rsrandom.h b/libretroshare/src/util/rsrandom.h index 5e7e0d92c..94d619537 100644 --- a/libretroshare/src/util/rsrandom.h +++ b/libretroshare/src/util/rsrandom.h @@ -48,7 +48,7 @@ public: static void random_bytes(uint8_t* data, uint32_t length); - /** Return a random alphanumeric string of the given lenght */ + /// Return a random alphanumeric *[0-9,A-Z,a-z] string of the given lenght static std::string alphaNumeric(uint32_t length); /** Return a random printable string of the given lenght */