digitalmars.D - Hide input string from stdin
- Michael Chen (3/3) May 22 2016 I tried to write a small program that receive string as password.
- Jonathan M Davis via Digitalmars-d (37/40) May 22 2016 If you're using *nix, then you can use ncurses.
- Era Scarecrow (5/9) May 22 2016 Hmmm if you don't mind flooding the console (with either a
- Steven Schveighoffer (3/11) May 23 2016 I admire your willingness to look at all approaches for this problem :)
- Wyatt (7/11) May 23 2016 For Linux, I think you could just use getpass() from
- Nemanja Boric (5/16) May 23 2016 Here's example `getpass` (in C++, but can be translated
I tried to write a small program that receive string as password. However, I didn't find available library for hide input string, even in core library. Any suggestion?
May 22 2016
On Sunday, May 22, 2016 22:38:46 Michael Chen via Digitalmars-d wrote:I tried to write a small program that receive string as password. However, I didn't find available library for hide input string, even in core library. Any suggestion?If you're using *nix, then you can use ncurses. http://code.dlang.org/packages/ncurses e.g. I have this function in one of my programs: string getPassword() { import deimos.ncurses.curses; import std.conv; enum bs = 127; enum enter = 10; initscr(); raw(); noecho(); timeout(-1); scope(exit) endwin(); printw("password: "); refresh(); dchar[] password; while(1) { immutable c = getch(); if(c == enter) break; if(c == bs) { if(!password.empty) { password = password[0 .. $ - 1]; password.assumeSafeAppend(); } } else password ~= c; } return to!string(password); } - Jonathan M Davis
May 22 2016
On Sunday, 22 May 2016 at 22:38:46 UTC, Michael Chen wrote:I tried to write a small program that receive string as password. However, I didn't find available library for hide input string, even in core library. Any suggestion?Hmmm if you don't mind flooding the console (with either a pattern or spaces or newlines), you could spawn a separate thread to spam the console until it gets the flag to quit... Certainly not the best idea, but it would work... (Probably).
May 22 2016
On 5/22/16 10:12 PM, Era Scarecrow wrote:On Sunday, 22 May 2016 at 22:38:46 UTC, Michael Chen wrote:I admire your willingness to look at all approaches for this problem :) -SteveI tried to write a small program that receive string as password. However, I didn't find available library for hide input string, even in core library. Any suggestion?Hmmm if you don't mind flooding the console (with either a pattern or spaces or newlines), you could spawn a separate thread to spam the console until it gets the flag to quit... Certainly not the best idea, but it would work... (Probably).
May 23 2016
On Sunday, 22 May 2016 at 22:38:46 UTC, Michael Chen wrote:I tried to write a small program that receive string as password. However, I didn't find available library for hide input string, even in core library. Any suggestion?For Linux, I think you could just use getpass() from core.sys.posix.unistd. Not sure what the Windows equivalent is. An agnostic, user-facing version isn't a terrible idea. Or arguably better, a good way to disable echo on stdin; maybe file a bug about this against std.stdio? -Wyatt
May 23 2016
On Monday, 23 May 2016 at 15:56:14 UTC, Wyatt wrote:On Sunday, 22 May 2016 at 22:38:46 UTC, Michael Chen wrote:Here's example `getpass` (in C++, but can be translated trivially) for Windows, using `SetConsoleMode` with turning off `ENABLE_ECHO_OUTPUT`. http://www.cplusplus.com/articles/E6vU7k9E/#WIN-e1I tried to write a small program that receive string as password. However, I didn't find available library for hide input string, even in core library. Any suggestion?For Linux, I think you could just use getpass() from core.sys.posix.unistd. Not sure what the Windows equivalent is. An agnostic, user-facing version isn't a terrible idea. Or arguably better, a good way to disable echo on stdin; maybe file a bug about this against std.stdio? -Wyatt
May 23 2016