MXS-1662: Move mxs_crypt into utils

Moved the mxs_crypt function into utils and renamed to mxs::crypt (no C
code used it).
This commit is contained in:
Markus Mäkelä 2018-09-10 15:19:18 +03:00
parent d2f31aab0a
commit 40d73948a9
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
3 changed files with 26 additions and 17 deletions

View File

@ -557,6 +557,16 @@ uint64_t get_byteN(const uint8_t* ptr, int bytes);
*/
uint8_t* set_byteN(uint8_t* ptr, uint64_t value, int bytes);
/**
* C++ wrapper function for the `crypt` password hashing
*
* @param password Password to hash
* @param salt Salt to use (see man crypt)
*
* @return The hashed password
*/
std::string crypt(const std::string& password, const std::string& salt);
/**
* Get kernel version
*

View File

@ -421,23 +421,6 @@ bool admin_linux_account_enabled(const char* uname)
return rv;
}
#define MXS_CRYPT_SIZE 60
void mxs_crypt(const char* password, const char* salt, char* output)
{
#if HAVE_GLIBC
struct crypt_data cdata;
cdata.initialized = 0;
char* pw = crypt_r(password, salt, &cdata);
snprintf(output, MXS_CRYPT_SIZE, "%s", pw);
#else
static std::mutex mxs_crypt_lock;
std::lock_guard<std::mutex> guard(mxs_crypt_lock);
char* pw = crypt(password, salt);
snprintf(output, MXS_CRYPT_SIZE, "%s", pw);
#endif
}
/**
* Add insecure remote (network) basic user.
*

View File

@ -42,6 +42,8 @@
#include <netinet/tcp.h>
#include <openssl/sha.h>
#include <thread>
#include <curl/curl.h>
#include <crypt.h>
#include <maxscale/alloc.h>
#include <maxscale/config.hh>
@ -1168,6 +1170,20 @@ int64_t get_total_memory()
namespace maxscale
{
std::string crypt(const std::string& password, const std::string& salt)
{
#if HAVE_GLIBC
struct crypt_data cdata;
cdata.initialized = 0;
return crypt_r(password.c_str(), salt.c_str(), &cdata);
#else
static std::mutex mxs_crypt_lock;
std::lock_guard<std::mutex> guard(mxs_crypt_lock);
std::string pw = crypt(password.c_str(), salt.c_str());
return pw;
#endif
}
std::string to_hex(uint8_t value)
{
std::string out;