www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Hide input string from stdin

reply Michael Chen <michael example.com> writes:
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
next sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
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
prev sibling next sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
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
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/22/16 10:12 PM, Era Scarecrow wrote:
 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).
I admire your willingness to look at all approaches for this problem :) -Steve
May 23 2016
prev sibling parent reply Wyatt <wyatt.epp gmail.com> writes:
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
parent Nemanja Boric <4burgos gmail.com> writes:
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:
 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
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-e1
May 23 2016