Skip to content

Commit 98975d9

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

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ HRESULT CUpdateCallbackConsole::CryptoGetTextPassword2(Int32 *passwordIsDefined,
830830
{
831831
if (AskPassword)
832832
{
833-
RINOK(GetPassword_HRESULT(_so, Password))
833+
RINOK(GetPasswordConfirm_HRESULT(_so, Password))
834834
PasswordIsDefined = true;
835835
}
836836
}
@@ -857,7 +857,7 @@ HRESULT CUpdateCallbackConsole::CryptoGetTextPassword(BSTR *password)
857857
if (!PasswordIsDefined)
858858
{
859859
{
860-
RINOK(GetPassword_HRESULT(_so, Password))
860+
RINOK(GetPasswordConfirm_HRESULT(_so, Password))
861861
PasswordIsDefined = true;
862862
}
863863
}

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

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,59 +60,120 @@ 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, const bool confirm)
118+
{
119+
UString confirmPsw;
120+
UString passInMsg;
121+
bool res = false;
105122

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+
if (confirm)
133+
{
134+
passInMsg = "Confirm password";
135+
OutputPassInputMessage(outStream, passInMsg);
136+
res = PassInput(confirmPsw);
137+
if (!res)
138+
{
139+
EndOutStream(outStream);
140+
return res;
141+
}
142+
143+
if (psw != confirmPsw)
144+
{
145+
if (outStream)
146+
{
147+
*outStream << '\n'
148+
<< "Confirm password is different from "
149+
<< "the initially entered password, stopping. "
150+
<< "Try again!";
151+
}
152+
res = false;
153+
}
154+
}
155+
156+
EndOutStream(outStream);
106157
return res;
107158
}
108159

109-
HRESULT GetPassword_HRESULT(CStdOutStream *outStream, UString &psw)
160+
static HRESULT HandleHRESULT(bool res, UString &psw)
110161
{
111-
if (!GetPassword(outStream, psw))
162+
if (!res)
112163
return E_INVALIDARG;
113164
if (g_StdIn.Error())
114165
return E_FAIL;
115166
if (g_StdIn.Eof() && psw.IsEmpty())
116167
return E_ABORT;
117168
return S_OK;
118169
}
170+
171+
HRESULT GetPasswordConfirm_HRESULT(CStdOutStream *outStream, UString &psw)
172+
{
173+
return HandleHRESULT(GetPassword(outStream, psw, true), psw);
174+
}
175+
176+
HRESULT GetPassword_HRESULT(CStdOutStream *outStream, UString &psw)
177+
{
178+
return HandleHRESULT(GetPassword(outStream, psw, false), psw);
179+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ enum EEnum
2323
NUserAnswerMode::EEnum ScanUserYesNoAllQuit(CStdOutStream *outStream);
2424
// bool GetPassword(CStdOutStream *outStream, UString &psw);
2525
HRESULT GetPassword_HRESULT(CStdOutStream *outStream, UString &psw);
26+
HRESULT GetPasswordConfirm_HRESULT(CStdOutStream *outStream, UString &psw);
2627

2728
#endif

0 commit comments

Comments
 (0)