【发布时间】:2013-04-02 15:16:13
【问题描述】:
我正在编写一个 Linux 应用程序,其中我必须使用 ncurses 从标准输入读取密码。我可以毫无问题地读入 C 风格的字符串,但是,这会带来安全风险,所以我必须找到一种方法来读入 STL 字符串(std::string)。 这是我的代码的相关部分:
initscr();
noecho();
string key;
... // Get the key from the user
string enter=key+"A"; // So the entered key is not the user-set one
while(enter!=key)
{
const char* msg="Key to unlock terminal? ";
move(y/2, (x-strlen(msg))/2);
erase();
printw(msg);
sscanw("%s", enter); // How do I read into an STL string?
}
【问题讨论】:
-
我不知道有什么办法可以直接做到这一点。我认为您必须编写一个循环来一次读取一个字符并将每个字符附加到 std::string。
-
ncurses似乎根本不支持std::string,即使在他们的 C++ API 中也是如此。我可以在源代码中找到对“字符串”的唯一引用包括 Cstring.h标头。 -
将密码读入普通 C 字符串会带来安全风险吗?
-
应该是锁屏的,像UNIX全屏终端一样,只能通过输入密码来关闭,而不是因为输入太多字符导致的缓冲区溢出或段错误
-
@john,现在我想起来了,我可以使用
getch()...
标签: c++ string stl ncurses stdstring