Skip to content

Commit 711b591

Browse files
committed
feature(console): password confirmation when creating encrypted archive
So that typos become less likely in the password.
1 parent e5431fa commit 711b591

File tree

1 file changed

+61
-13
lines changed

1 file changed

+61
-13
lines changed

CPP/7zip/UI/Console/UserInputUtils.cpp

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,97 @@ NUserAnswerMode::EEnum ScanUserYesNoAllQuit(CStdOutStream *outStream)
6060
#endif
6161
#endif
6262

63-
static bool GetPassword(CStdOutStream *outStream, UString &psw)
63+
static void OutputPassInputMessage(CStdOutStream *outStream, const UString &msg)
6464
{
6565
if (outStream)
6666
{
67-
*outStream << "\nEnter password"
67+
*outStream << '\n' << msg
6868
#ifdef MY_DISABLE_ECHO
69-
" (will not be echoed)"
69+
<< " (will not be echoed)"
7070
#endif
71-
":";
71+
<< ":";
7272
outStream->Flush();
7373
}
74+
}
75+
76+
static bool PassInput(UString &psw)
77+
{
78+
bool res = false;
7479

7580
#ifdef MY_DISABLE_ECHO
76-
77-
const HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
7881

7982
/*
8083
GetStdHandle() returns
8184
INVALID_HANDLE_VALUE: If the function fails.
8285
NULL : If an application does not have associated standard handles,
8386
such as a service running on an interactive desktop,
8487
and has not redirected them. */
85-
bool wasChanged = false;
88+
const HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
8689
DWORD mode = 0;
90+
bool wasChanged = false;
91+
8792
if (console != INVALID_HANDLE_VALUE && console != NULL)
8893
if (GetConsoleMode(console, &mode))
8994
wasChanged = (SetConsoleMode(console, mode & ~(DWORD)ENABLE_ECHO_INPUT) != 0);
90-
const bool res = g_StdIn.ScanUStringUntilNewLine(psw);
95+
res = g_StdIn.ScanUStringUntilNewLine(psw);
9196
if (wasChanged)
9297
SetConsoleMode(console, mode);
93-
94-
#else
95-
96-
const bool res = g_StdIn.ScanUStringUntilNewLine(psw);
97-
98+
99+
# else
100+
101+
res = g_StdIn.ScanUStringUntilNewLine(psw);
102+
98103
#endif
99104

105+
return res;
106+
}
107+
108+
static void EndOutStream(CStdOutStream *outStream)
109+
{
100110
if (outStream)
101111
{
102112
*outStream << endl;
103113
outStream->Flush();
104114
}
115+
}
116+
117+
static bool GetPassword(CStdOutStream *outStream, UString &psw)
118+
{
119+
UString confirmPsw;
120+
UString passInMsg;
121+
bool res = false;
122+
123+
passInMsg = "Enter password";
124+
OutputPassInputMessage(outStream, passInMsg);
125+
res = PassInput(psw);
126+
if (!res)
127+
{
128+
EndOutStream(outStream);
129+
return res;
130+
}
131+
132+
passInMsg = "Confirm password";
133+
OutputPassInputMessage(outStream, passInMsg);
134+
res = PassInput(confirmPsw);
135+
if (!res)
136+
{
137+
EndOutStream(outStream);
138+
return res;
139+
}
140+
141+
if (psw != confirmPsw)
142+
{
143+
if (outStream)
144+
{
145+
*outStream << '\n'
146+
<< "Confirm password is different from "
147+
<< "the initially entered password, stopping. "
148+
<< "Try again!";
149+
}
150+
res = false;
151+
}
105152

153+
EndOutStream(outStream);
106154
return res;
107155
}
108156

0 commit comments

Comments
 (0)