From 74c2179c55afd7a5a9ac0993fa2dbe122e02abb7 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 15 May 2015 17:28:34 +0200 Subject: [PATCH] Don't echo written password under win32 --- libdevcore/CommonIO.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index d37f74db2..9538ca55f 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -24,7 +24,10 @@ #include #include #include "Exceptions.h" -#ifndef _WIN32 +#include +#ifdef _WIN32 +#include +#else #include #endif using namespace std; @@ -125,8 +128,22 @@ std::string dev::getPassword(std::string const& _prompt) { #if WIN32 cout << _prompt << flush; + // Get current Console input flags + HANDLE hStdin; + DWORD fdwSaveOldMode; + if ((hStdin = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) + BOOST_THROW_EXCEPTION(ExternalFunctionFailure("GetStdHandle")); + if (!GetConsoleMode(hStdin, &fdwSaveOldMode)) + BOOST_THROW_EXCEPTION(ExternalFunctionFailure("GetConsoleMode")); + // Set console flags to no echo + if (!SetConsoleMode(hStdin, fdwSaveOldMode & (~ENABLE_ECHO_INPUT))) + BOOST_THROW_EXCEPTION(ExternalFunctionFailure("SetConsoleMode")); + // Read the string std::string ret; std::getline(cin, ret); + // Restore old input mode + if (!SetConsoleMode(hStdin, fdwSaveOldMode)) + BOOST_THROW_EXCEPTION(ExternalFunctionFailure("SetConsoleMode")); return ret; #else struct termios oflags;