Break out random ID methods
The method of creating a random ID from an openssl random generator of a
particular length is something that is generally useful, and something
we can write unit tests for. Add it.
Tested:
Redfish service validator login flows work correctly in redfish service
validator.
Change-Id: Ic3b58d33f1421f3eb39e2d57585958f87f6fb8ea
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/src/ossl_random.cpp b/src/ossl_random.cpp
index 1261977..419fe31 100644
--- a/src/ossl_random.cpp
+++ b/src/ossl_random.cpp
@@ -1,14 +1,63 @@
#include "ossl_random.hpp"
+extern "C"
+{
+#include <openssl/rand.h>
+}
+
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
+#include <array>
+#include <random>
#include <string>
-std::string bmcweb::getRandomUUID()
+namespace bmcweb
+{
+uint8_t OpenSSLGenerator::operator()()
+{
+ uint8_t index = 0;
+ int rc = RAND_bytes(&index, sizeof(index));
+ if (rc != opensslSuccess)
+ {
+ BMCWEB_LOG_ERROR("Cannot get random number");
+ err = true;
+ }
+
+ return index;
+}
+
+std::string getRandomUUID()
{
using bmcweb::OpenSSLGenerator;
OpenSSLGenerator ossl;
return boost::uuids::to_string(
boost::uuids::basic_random_generator<OpenSSLGenerator>(ossl)());
}
+
+std::string getRandomIdOfLength(size_t length)
+{
+ static constexpr std::array<char, 62> alphanum = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
+ 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
+ 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+ 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
+
+ std::string token;
+ token.resize(length, '0');
+ std::uniform_int_distribution<size_t> dist(0, alphanum.size() - 1);
+
+ bmcweb::OpenSSLGenerator gen;
+
+ for (char& tokenChar : token)
+ {
+ tokenChar = alphanum[dist(gen)];
+ if (gen.error())
+ {
+ return "";
+ }
+ }
+ return token;
+}
+} // namespace bmcweb