diff --git a/Utils.cpp b/Utils.cpp index 12760950..3ec177a7 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -1,4 +1,5 @@ #include "Utils.h" +#include "md5.h" #include #include #include @@ -218,6 +219,36 @@ int CUtils::MakeDir(const string& sPath, mode_t iMode) { return -1; } +string CUtils::GetHashPass() { + while (true) { + char* pass = CUtils::GetPass("Enter Password"); + char* pass1 = (char*) malloc(strlen(pass) +1); + strcpy(pass1, pass); // Make a copy of this since it is stored in a static buffer and will be overwritten when we fill pass2 below + memset((char*) pass, 0, strlen(pass)); // null out our pass so it doesn't sit in memory + char* pass2 = CUtils::GetPass("Confirm Password"); + int iLen = strlen(pass1); + + if (strcmp(pass1, pass2) != 0) { + CUtils::PrintError("The supplied passwords did not match"); + } else if (!iLen) { + CUtils::PrintError("You can not use an empty password"); + } else { + string sRet((const char*) CMD5(pass1, iLen)); + memset((char*) pass1, 0, iLen); // null out our pass so it doesn't sit in memory + memset((char*) pass2, 0, strlen(pass2)); // null out our pass so it doesn't sit in memory + free(pass1); + + return sRet; + } + + memset((char*) pass1, 0, iLen); // null out our pass so it doesn't sit in memory + memset((char*) pass2, 0, strlen(pass2)); // null out our pass so it doesn't sit in memory + free(pass1); + } + + return ""; +} + char* CUtils::GetPass(const string& sPrompt) { PrintPrompt(sPrompt); return getpass(""); diff --git a/Utils.h b/Utils.h index 3836340e..dea76135 100644 --- a/Utils.h +++ b/Utils.h @@ -35,6 +35,7 @@ public: static void PrintPrompt(const string& sMessage); static void PrintAction(const string& sMessage); static void PrintStatus(bool bSuccess, const string& sMessage = ""); + static string GetHashPass(); static char* GetPass(const string& sPrompt); static bool GetInput(const string& sPrompt, string& sRet, const string& sDefault = ""); static bool GetBoolInput(const string& sPrompt, bool bDefault);