Masking Password with *'s


 
Thread Tools Search this Thread
Top Forums Programming Masking Password with *'s
# 22  
Old 12-19-2010
Quote:
Originally Posted by Corona688
That's two backspaces, with a space between them. I don't think you can print a delete and get anything sensible. I can't see your computer from here, please post your code.
I meant I would print "\b \b", but I just want to give the user the option of deleting characters with backspace or delete.

So I was messing around with implementing backspace and also reading it into into an array today and think I've got it working fairly well. Correct me if I'm wrong, but getchar() turns the input into a unique integer right? Well I want to display the password that was entered just to check, but if everything has been converted into integers, how do I display it?

And I'll narrow my earlier question changing the colors into something that is answerable. Basically I want to do something along these lines:
Code:
  while ((ch = getchar()) != '\n')
   {
      if(ch == '\b')     /*Here is where I want to give the option of using delete*/
      {
         printf("\b \b");
         i--;
      }

      else
      {
         password[i] = ch;

         if((ch == '1')    /*I'm not sure if this is done correcting. I want to print a red "1" if a 1 was typed*/
         {
            printf("\033[1;3;31;49m1\033[0m");         /*Output a red '1'*/
         }

         else((ch == '2')    /*Same issue*/
         {
            printf("\033[1;3;34;49m2\033[0m");         /*Output a blue '2'*/
         }

         else((ch == '3')     /*Same issue*/
          {
             printf("\033[1;3;32;49m3\033[0m");         /*Output a green '3'*/
          }

         i++;
      }
   }

I guess this is kinda similar to my first question, this time comparing something that has been turned into an integer to an input. I commented on the lines I am referring to.
# 23  
Old 12-19-2010
[QUOTE=bigdrock44;302481777]I meant I would print "\b \b", but I just want to give the user the option of deleting characters with backspace or delete.

So I was messing around with implementing backspace and also reading it into into an array today and think I've got it working fairly well. Correct me if I'm wrong, but getchar() turns the input into a unique integer right? Well I want to display the password that was entered just to check, but if everything has been converted into integers, how do I display it?

And I'll narrow my earlier question changing the colors into something that is answerable. Basically I want to do something along these lines:
Code:
  while ((ch = getchar()) != '\n')
   {
      if(ch == '\b')     /*Here is where I want to give the option of using delete*/
      {
         printf("\b \b");
         i--;
      }

What this does is print one backspace, which moves the cursor back one. Then it prints a space, which prints a blank overtop of the spot it moved to(which doesn't happen just from printing a backspace). then it backspaces again.

But that probably won't work since you're using printf, which is buffered! It'll wait until you feed it a \n to print anything. Try fprintf(stderr, "\b \b"); instead.
Code:
      else
      {
         password[i] = ch;

         if((ch == '1')    /*I'm not sure if this is done correcting. I want to print a red "1" if a 1 was typed*/
         {
            printf("\033[1;3;31;49m1\033[0m");         /*Output a red '1'*/
         }

That code works absolutely fine on my system... (again, though, it's buffered -- fprintf(stderr, "...); instead) but what I'd do is:

Code:
if((ch >= '0') && (ch <= '9'))
{
   fprintf(stderr, "\033[1;3;31;49m%c\033[0m", ch);
}

It also lets you add as many more red characters as you want just by expanding that one if statement.

If it still doesn't work for you, please be specific! Post the exact error message and the exact line!

Last edited by Corona688; 12-20-2010 at 12:08 AM.. Reason: buffering
# 24  
Old 12-21-2010
I got it to do what I was basically asking. Here's the issue, though. I want it to output the "*" or the colored numbers, whatever the situation may be, but I want to store the actual keystroke. For example, here's my code:
Code:
void read_unbuffered(void)
{
   int ch;           
   int password[4];
   int i;   

   setbuf(stdin, NULL);    
   echo_off();

   i = 0; 

   while ((ch = getchar()) != '\n')
   {      
      if(ch == '\b' && i != 0)
      {   
         fprintf(stderr, "\b\b  \b\b");        /*Don't mind this. As you can see below 
         i--;                                            after each entry it puts a space (for clarity), so I double backspace 
                                                           and double space to make up for this*/ 
      }

      else if((ch >= '1') && (ch <= '6') && i < 4)
      {
         password[i] = ch;

         if(ch == '1')
         {
            fprintf(stderr, "\033[1;3;31;49m%c \033[0m", ch);
         }

         else if(ch == '2')
         {
            fprintf(stderr, "\033[1;3;34;49m%c \033[0m", ch);
         }

         else if(ch == '3')
         {
            fprintf(stderr, "\033[1;3;33;49m%c \033[0m", ch);
         }

         else if(ch == '4')
         {           
            fprintf(stderr, "\033[1;3;35;49m%c \033[0m", ch);
         }  

         else if(ch == '5')
         {    
            fprintf(stderr, "\033[1;3;32;49m%c \033[0m", ch);
         }

         else if(ch == '6')        
         {
            fprintf(stderr, "\033[1;3;39;49m%c \033[0m", ch);
         }

         i++;
      }
   }

   printf("\n");

   for(i = 0; i < 4; i++)           /*To see what is stored in the array*/
   {
      printf("%d ", password[i]);
   }

   echo_on();
}

Say I type in "1234". This is what happens on the screen when I type that:
Code:
1 2 3 4     /*This is good. This is what I want. To showed colored numbers as I type.*/
49 50 51 52     /*These are the values stored in the array*/

So the code works well in that as I type in 1234, it is actively changing the color as I type. Since the code is using getchar(), however, it is storing the values 49, 50, 51, and 52 in the array. I want 1, 2, 3, and 4 to be stored in the array, because basically I want this function to return this array, and if it is returning an array of 49 50 51 52 it will not make sense in my program. Does this make sense?
# 25  
Old 12-21-2010
Code:
void read_unbuffered(void)
{
   int ch;
   int password[4];
   int i;
   static const int color[]={0, 31, 34, 33, 35, 32, 39};

   setbuf(stdin, NULL);    
   echo_off();

   i = 0; 

   while ((ch = getchar()) != '\n')
   {      
      if(ch == '\b' && i != 0)
      {   
         fprintf(stderr, "\b\b  \b\b");        /*Don't mind this. As you can see below 
         i--;                                            after each entry it puts a space (for clarity), so I double backspace 
                                                           and double space to make up for this*/ 
      }

      else if((ch >= '1') && (ch <= '6') && i < 4)
      {
         // 1 is not '1'.  An ASCII 1, like you type in, is 49 in decimal.
         // An ASCII 9 is 57.
         // ASCII has them all in a row, so '5' - '0' == 5, etc.
         password[i] = ch-'0';

         // Use an array so you don't need umpteen different print statements
         fprintf(stderr, "\033[1;3;%d;49m%c \033[0m", color[ch-'0'], ch);

         i++;
      }
   }

   printf("\n");

   for(i = 0; i < 4; i++)           /*To see what is stored in the array*/
   {
      printf("%d ", password[i]);
   }

   echo_on();
}


Last edited by Corona688; 12-21-2010 at 09:12 PM.. Reason: whoops, not all printf is the same...
# 26  
Old 12-21-2010
Oh okay, yeah I noticed that it was along those lines but I didn't know if it would be wrong to do that.

So now I have everything working correctly, I just want the function to return an array, not be void. But as I'm thinking about it it's not possible to return an array right? I must return a pointer? I'm not really sure how this works but is it along these lines?
Code:
int password[4];

int* read_unbuffered(void)
{
   /*code*/
  
   return password;
}

int main()
{
   password = read_unbuffered();

   return 0;
}


Last edited by bigdrock44; 12-21-2010 at 10:29 PM..
# 27  
Old 12-22-2010
The difference between an array and a pointer is nil as far as your code's concerned. They both work with the [] operator the exact same way.

Instead of using a global variable, which works but is ugly and unsafe, you should pass a pointer into the function for it to use. This also leaves room for a proper return value to check if an error happened.
Code:
int read_unbuffered(int *password)
{
    ...
    if(success)
        return(1);
    else
        return(0);
}

int main(void)
{
    int arr[4];

    if(read_unbuffered(arr))
    {
        fprintf(stderr, "read pass\n");
    }
    else
    {
       fprintf(stderr, "couldn't read pass\n");
    }
    return(0);
}

# 28  
Old 12-22-2010
Well I'm actually not using it as a typical password that grants access to something. I'm making a Mastermind game, maybe you've heard of it. Basically, one player types a code and the other player has to figure out what it was based on some given hints. The whole thing is done, I just had to cover up the code with *'s so that the other player wouldn't be able to see on the screen what it was.

So I need to somehow return the array from the read_unbuffered() function so that I can continue with my program which is currently comparing what the Codebreaker input to that of the Codemaker. If it's not possible, I can just copy the whole functions code into what I already have, but for clarity and legibility I was trying to just utilize it as a separate function.
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