1

Possible Duplicate:
Hide password input on terminal

please give me a solution for a password program in Linux using C language .

when we entering the first letter of password the window should be shows

password:*-

instead of the entered letter.

when we enter the next character the window should be shows

password**-

up to 8 character and also verify the password.

Cœur
  • 37,241
  • 25
  • 195
  • 267
TamiL
  • 2,706
  • 4
  • 24
  • 44

4 Answers4

2
read -s -n 8 -p "password:" mypassword

-s makes it a password input prompt
-n limits the number of characters
-p STRING sets the prompt

The inputted password is saved in the $mypassword variable for further use.

It's usual on the linux command line that no substitute characters are displayed when inputting a password, so this command doesn't offer an option to do that. Still, it's a good approximation to what you are looking for.

Chris Ortner
  • 806
  • 6
  • 11
  • 1
    He clearly stated C language, not shell. however, it's still useful to be called via POSIX system("read ...") call. – Luka Ramishvili Jul 30 '12 at 06:55
  • @LukaRamishvili: You meant `popen`? I don't see how is it useful with `system`. Hints? – jweyrich Jul 30 '12 at 07:52
  • Yep you're right, if he wants to read back the entered password, he needs to open a pipe, not just call (though the visual effect can be achieved using the system() call, it probably won't be sufficient). – Luka Ramishvili Jul 30 '12 at 11:32
2

may be this could help you

http://www.askdavetaylor.com/how_to_read_password_without_echoing_c.html

ganapathydselva
  • 416
  • 6
  • 13
2

Using C features :

/* no test */
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

static void 
changeMode(bool b)
{
    static struct termios cooked;
    static int raw_actived = 0;

    if (raw_actived == b) return;
    if (b) {
        struct termios raw;

        tcgetattr(STDIN_FILENO, &cooked);
        raw = cooked;
        cfmakeraw(&raw);
        tcsetattr(STDIN_FILENO, TCSANOW, &raw);
    } else {
        tcsetattr(STDIN_FILENO, TCSANOW, &cooked);
    }   
    raw_actif = b;
}

static void
clean(void)
{
    int c;
    do
        c = getchar();
    while (c != '\n' && c != EOF);
}

void
askPassword(char *s, size_t n)
{
    changeMode(1);
    for (size_t i = 0; i < n; ++i) {
        s[i] = getchar();
        clean();
        putchar('*');
    }
    changeMode(0);
}
md5
  • 23,373
  • 3
  • 44
  • 93
1

This is just an idea, so you can try or not.

At the very begining, you have to turn the terminal into non-echo mode. So that, the user's input won't be showed. (Termios settings)

Start with a variable counter. You create a loop (while) and in it, you use read function or getchar to get every single input. Next, you check if the input is right or not. Writing on the terminal the '*' or nothing.

babttz
  • 446
  • 4
  • 11