Masking Password with *'s


 
Thread Tools Search this Thread
Top Forums Programming Masking Password with *'s
# 29  
Old 12-22-2010
Quote:
Originally Posted by bigdrock44
for clarity and legibility I was trying to just utilize it as a separate function.
But it is a seperate function. I don't understand.

If you don't care about thread safety, you could use a global variable (or a static, local variable) and return an "int *". But this isn't reccomended because it's not threadsafe. When you look for examples of how it's done in system libraries, you see all sorts of things like sprintf(), fgets() and the like which all take buffers you give them. And the ones which don't, like gets(), are much maligned.
# 30  
Old 12-22-2010
Like you said, it is a separate function, but I need to utilize it so that it returns the array that the user typed in. I don't want a simple true or false to be returned because that is not what I'm doing. I know I use the word "password" but it's more so a code. I'll try to give a basic idea of what my program does. I'll just pseudo code it.

Code:
int main()
{
   /*Prompt player one for secret code*/
   codemaker[4] = read_unbuffered();              /*Here is where I want to use read_unbuffered to somehow return the array.*/

   /*Comper codemaker's secret code to the codebreaker's guess*/
   for (i = 0; i < 4; i++)
   {
       codemaker[i] = codebreaker[i];         /*This is way simplified and wouldn't even work, but just to give a vague idea of what's going on.
    }

   printKey();        /*Prints a key so that the codebreaker can see what's right and wrong about his guess*/

   return 0;
}

As you can see, I use codemaker[] throughout the program and I need it to be set to the array that is created in read_unbuffered.
# 31  
Old 12-22-2010
But why do you need it that way? You use it throughout the program, so what? Passing it into the function doesn't destroy it or alter its scope.
# 32  
Old 12-22-2010
Let me make sure I understand everything correctly first. When you have variables within a function, they are local to that function correct? It will not have any affect on the variables within the main, right? So in my read_unbuffered() I'm using password[] and in my main I'm using codeMaker[]. These have no bearing on each other right?
# 33  
Old 12-22-2010
Not when password is a pointer to codeMaker. Then it'll alter codeMaker.
# 34  
Old 12-23-2010
So if I want to alter codeMaker (located in main()) with the function read_unbuffered I should not use a pointer?

Here's an excerpt from my main:
Code:
   /*Determine if code is valid*/
   valid = 0;

   while(valid != 1)
   {
      printf("Player 2, enter a secret code using values 1-6: ");

      getcode();

      /*Determine if any of the inputed coordinates are invalid*/
      for(i = 0; i < 4; i++)
      {      
         /*If invalid, warn user and re-prompt*/
         if(codeMaker[i] < 1 || codeMaker[i] > 6)     /*This codeMaker is defined in the main*/
         {
            printf("Please enter values from 1 to 6!\n\n");

            i = 4;
         }

         /*If valid AND last coordinate, continue on*/
         else if(i == 3)
         {
            valid = 1;
         }
      }
   }

And, just to refresh, here is the function:
Code:
void getcode()
{
   int ch;
   int i;
   int codeMaker[4];

   setbuf(stdin, NULL);
   echo_off();

   i = 0;

   while ((ch = getchar()) != '\n')
   {
      if(ch == '\b' && i != 0)
      {
         fprintf(stderr, "\b\b  \b\b");
         i--;
         codeMaker[i] = 0;
      }

      else if((ch >= '1') && (ch <= '6') && i < 4)
      {
         codeMaker[i] = ch - '0';
         fprintf(stderr, "* ");
         i++;
      }
   }

   printf("\n");

   echo_on();
}

As you can see, I've changed the name of read_unbuffered() to getcode(). Also in getcode() I changed the array from password[] to codeMaker[] in attempt to make this work.

So, since the codeMaker[] in getcode() is local to getcode(), it is not affecting the codeMaker[] in main(), right? Which means, when I check for validity in main() it is checking a different codeMaker[] that it needs to be checking.
# 35  
Old 12-23-2010
Quote:
Originally Posted by bigdrock44
So if I want to alter codeMaker (located in main()) with the function read_unbuffered I should not use a pointer?
That's the precise opposite of what I just told you. You're confusing yourself with too much doublethink.

Pass a pointer to codeMaker into read_unbuffered. Just like the way dozens of stdio functions take a pointer to a buffer, to let them read or write to that buffer. That tells it what memory it should modify, so it can modify the contents of that array directly without passing anything back (because returning an array is nonsensical, and returning a pointer doesn't modify its contents, so both are useless in this context). As long as the memory's valid, a pointer to memory doesn't care about variable scope.
Quote:
So, since the codeMaker[] in getcode() is local to getcode(), it is not affecting the codeMaker[] in main(), right?
Correct. It bears no resemblance to the example I showed you, however, which does modify variables outside itself. I'll flesh it out a little more.

Code:
void read_unbuffered(int *password)
{
   int ch;
   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();
}

int password_global[4];

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

        // giving a pointer means no need to return anything -- the memory is modified directly.
        read_unbuffered(password_local); // modifies password_local
        read_unbuffered(password_global); // modifies password_global
}


Last edited by Corona688; 12-23-2010 at 01:28 PM..
This User Gave Thanks to Corona688 For This Post:
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