Masking Password with *'s


 
Thread Tools Search this Thread
Top Forums Programming Masking Password with *'s
# 1  
Old 12-13-2010
Question Masking Password with *'s

So I've been working on this for some time now and can't seem to find the solution that works for me. I'm working in C/Unix. Basically, I want to take a user input and output something different. For example, I want to take a password and output *'s. In another instance, I want to take inputed numbers and output colored ones. I figured the colored numbers is the same as the password one, just using if statements.

Here's a code that I found online that seems to almost do the job, except I need it to output *'s as the user is typing something. Instead, it just stays blank.


Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

struct termios save;

void echoOff()
{
  struct termios term;

  tcgetattr( 0, &save );
  term = save;
  term.c_lflag &= (~ECHO);
  tcsetattr( 0, TCSANOW, &term );
}

void echoOn()
{
  tcsetattr( 0, TCSANOW, &save );
}

int main()
{
  char password[30];

  echoOff();
  fgets( password, 30, stdin );
  echoOn();

  printf("Hello dude, your Password was: %s\n", password );

  return 0;
}

Can anyone help me with this? Thanks!

Last edited by pludi; 12-13-2010 at 06:46 AM..
# 2  
Old 12-13-2010
Code:
// this one line
fgets( password, 30, stdin );
// could be like this for example:
int ch=0;
while( (ch=fgetc(stdin)) !=EOF)
{
    printf("%c", (ch=='\n') ? ch : '*');
    if(ch=='\n') break;
}
// .... rest of code

# 3  
Old 12-14-2010
Okay I tried that, but when I type the password it still stays blank, only showing the stars after I press enter instead of doing it after each keystroke. Also, it's not even passing the password into the password array. For example if I type in 12345, it stays blank, then I press enter and it shows the five *'s, then it says "Hello dude, your Password was: " without the 12345 after the colon. Thanks for the help by the way!
# 4  
Old 12-14-2010
If you want to read raw keystrokes you'll have to mess with your terminal and use read() instead of stdio.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-14-2010
my bad - this works, my code had several logic errors - thanks corona.

Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>

struct termios save;

void echoOff()
{
  struct termios term;

  tcgetattr( 0, &save );
  term = save;
  term.c_lflag &= ~(ICANON | ECHO);;
  term.c_cc[VMIN]=0;
  term.c_cc[VTIME]=0;
  
  tcsetattr( 0, TCSANOW, &term );
}

void echoOn()
{
  tcsetattr( 0, TCSANOW, &save );
}

int kbd_rd(const int fd)  /* wait 2 seconds to read keyboard stroke */
{
    fd_set set_read;
    int maxfd = fd + 1;
    int ch=0;
    char buf[4]={0x0};
    struct timeval tv_tmout={0,0};
    tv_tmout.tv_sec=99;
    FD_ZERO(&set_read);
    FD_SET(fd, &set_read);
    select(maxfd, &set_read, NULL, NULL, &tv_tmout);
    if (FD_ISSET(fd, &set_read))
        read(fd, buf, 1);
    ch=buf[0];

    return ch;
}


int main()
{
  char password[30]={0x0};
  char *p=password;
  int ch=0;
  int fd=fileno(stdin);
  
  echoOff();
  while( (ch=kbd_rd(fd)) )
  {
      printf("%c", (ch=='\n') ? ch : '*');
      fflush(stdout);
      if(ch=='\n') break;
      *p=ch;
      p++;
  }
  
  echoOn();

  printf("Hello dude, your Password was: %s\n", password );

  return 0;
}

There is no error checking, plus tcsetattr() will return success even if it does not set all the attributes correctly. See 'Advanced Programming in the UNIX Environment' - Stevens & Rago - section on terminal I/O/
# 6  
Old 12-14-2010
I'm actually getting the same results as the first code you posted. I was wondering, would getc() and putc() work? I thought getc() didn't echo and I could basically do a loop using getc(stdin) and putc('*', stdout). I could never get it to work though...
# 7  
Old 12-15-2010
I tested the code I wrote on Solaris 10 and ubuntu. What did you do exactly? Please post your code.

And no, fgetc is not any better. Corona wqs right to start with - you need low-level I/O. I was asleep at the switch.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Masking with gsub command

My file "test.dat" data as below Requirement is to mask(replace) all english characters with "X" EXCEPT first 7 characters of every line. my command awk '{gsub("]","X")}1' test.dat looks not working properly, Appreciate any suggestion... (6 Replies)
Discussion started by: JSKOBS
6 Replies

2. UNIX for Dummies Questions & Answers

Masking data

How Can I mask one particular columns using some unix command? (4 Replies)
Discussion started by: dsa
4 Replies

3. Shell Programming and Scripting

Masking algorithm

I have a requirement of masking few specific fields in the UNIX file. The details are as following- File is fixed length file with each record of 250 charater length. 2 fields needs to be masked – the positions are 21:30 and 110:120 The character by character making needs to be done which... (5 Replies)
Discussion started by: n78298
5 Replies

4. Shell Programming and Scripting

Masking Password from within a Bash Shell Script

Is there a way to mask the password inside of a script to minimize the impact of a comprimised server? So ssh -o "PasswordAuthentication no" -o "HostbasedAuthentication yes" -l testuser 192.168.3.1 "mysqldump --opt --all-databases -u root -pPassword| gzip" > $backup_dir/mysqldump.gz a... (2 Replies)
Discussion started by: metallica1973
2 Replies

5. Shell Programming and Scripting

masking issue

Hi I am facing an issue with the below script which has the below line each field being separated with a tab. I need to mask the 8 and 7th field based on following conditions 1. 8th field is 16 in length and is numerics i will mask the middle 6 digits except the first 6 and last 4. input... (2 Replies)
Discussion started by: mad_man12
2 Replies

6. Shell Programming and Scripting

Scripting help/advise on hiding/masking username/password

Hi, I currently have a UNIX script with a function that uses a username and password to connect to the database, retrieve some information and then exit. At the moment, am getting the username and password from a hidden plain text file and permission set to -r--------, i.e. read only to who... (1 Reply)
Discussion started by: newbie_01
1 Replies

7. Emergency UNIX and Linux Support

Masking of number

BAT:0310:2009-08-0:Y4 :H:D:00003721:03103721.IFH:00138770:05767:00000000001279' EXR:CLP:912.570000' STA:A:9071559:2009-08-10::Wer::Mrs' DEF::531.97:531.97:310221661617::+ABC:BAL:1:N::::5:40.00:0.00:2009-08-10:CN:1111111111109962::3:N:missc :N:PH:00010833:... (5 Replies)
Discussion started by: mad_man12
5 Replies

8. Shell Programming and Scripting

Data Masking

I have a pipe delimited file that I need to 'mask' to before loading to keep some data confidential. I need to maintain the first 4 bytes of certain columns and replace the remaining bytes with an 'x'. I would like to maintain spaces but it's not a requirement. Example, need to mask columns 2... (2 Replies)
Discussion started by: 1superdork
2 Replies

9. Shell Programming and Scripting

Masking Content of a String

Hello, I need to know that whether a content of a string can be hidden or masked inside a shell script. My Sample Code is given below <Code> #!/usr/bin/ksh Userid=test DB=temp Passwd=`java Decryption test` # The Above command will get the encryped password for "test" user id and store... (2 Replies)
Discussion started by: maxmave
2 Replies

10. IP Networking

IP Masking

Is it possible for a internal LAN to mask a IP e.g. i have a server ip running the intranet ip being 192.168.0.8 and i want to make that like www.intranet.com is this possible on a internal network ? (1 Reply)
Discussion started by: perleo
1 Replies
Login or Register to Ask a Question