Hide input string from stdin

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sun May 22 16:27:38 PDT 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



More information about the Digitalmars-d mailing list