From 895b5091904bee1eed34377aa52d1a8abfbe2fa8 Mon Sep 17 00:00:00 2001 From: Markus Uhlin Date: Mon, 25 Dec 2023 07:01:46 +0100 Subject: fics_getsalt() -- initial revision --- FICS/fics_getsalt.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ FICS/fics_getsalt.h | 17 +++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 FICS/fics_getsalt.cpp create mode 100644 FICS/fics_getsalt.h (limited to 'FICS') diff --git a/FICS/fics_getsalt.cpp b/FICS/fics_getsalt.cpp new file mode 100644 index 0000000..95705b0 --- /dev/null +++ b/FICS/fics_getsalt.cpp @@ -0,0 +1,46 @@ +/* + * Written by Markus Uhlin , + * 25 Dec 2023. + */ + +#include +#include +#include + +#include "fics_getsalt.h" + +char * +fics_getsalt(void) +{ + static bool init_done = false; + static const char legal_index[] = + "./0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + static char salt[FICS_SALT_SIZE]; + + if (!init_done) { + strcpy(salt, FICS_SALT_BEG); + init_done = true; + } + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dist(0, strlen(legal_index) - 1); + + for (size_t i = 7; i < sizeof salt; i++) + salt[i] = legal_index[dist(gen)]; + salt[sizeof salt - 1] = '\0'; + return (&salt[0]); +} + +#if SELF_TEST +int +main(void) +{ + puts(fics_getsalt()); + puts(fics_getsalt()); + puts(fics_getsalt()); + return 0; +} +#endif diff --git a/FICS/fics_getsalt.h b/FICS/fics_getsalt.h new file mode 100644 index 0000000..c22f9c8 --- /dev/null +++ b/FICS/fics_getsalt.h @@ -0,0 +1,17 @@ +#ifndef FICS_GETSALT_H +#define FICS_GETSALT_H + +#define FICS_SALT_BEG "$2b$10$" +#define FICS_SALT_SIZE 30 + +#ifdef __cplusplus +extern "C" { +#endif + +char *fics_getsalt(void); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3