0

Slightly similar to this question, I want to replace argv contents:

string argv = "-help=none\n-URL=(default)\n-password=look\n-uname=Khanna\n-p=100";

to this:

"-help=none\n-URL=(default)\n-password=********\n-uname=Khanna\n-p=100"

I have tried very basic string find and search operations (using IndexOf, SubString etc.). I am looking for more elegant solution so as to replace this part of string:

-password=AnyPassword

to:

-password=*******

And keep other part of string intact. I am looking if String.Replace or Regex replace may help.

What I've tried (not much of error-checks):

var pwd_index = argv.IndexOf("--password=");

string converted;

if (pwd_index >= 0)
{
     var leftPart = argv.Substring(0, pwd_index);
     var pwdStr = argv.Substring(pwd_index);
     var rightPart = pwdStr.Substring(pwdStr.IndexOf("\n") + 1);

     converted = leftPart + "--password=********\n" + rightPart;
}
else
     converted = argv;

Console.WriteLine(converted);

2 Answers 2

3

Solution

Similar to Rubens Farias' solution but a little bit more elegant:

string argv = "-help=none\n-URL=(default)\n-password=\n-uname=Khanna\n-p=100";
string result = Regex.Replace(argv, @"(password=)[^\n]*", "$1********");

It matches password= literally, stores it in capture group $1 and the keeps matching until a \n is reached.

This yields a constant number of *'s, though. But telling how much characters a password has, might already convey too much information to hackers, anyway.

Working example: https://dotnetfiddle.net/xOFCyG

Regular expression breakdown

(              // Store the following match in capture group $1.
  password=    // Match "password=" literally.
)    
[              // Match one from a set of characters.
  ^            // Negate a set of characters (i.e., match anything not
               //   contained in the following set).
    \n         // The character set: consists only of the new line character.
]
*              // Match the previously matched character 0 to n times.
Sign up to request clarification or add additional context in comments.

2 Comments

Elegant and readable solution. And thanks for parameter-breakdown! :)
Add RegexOptions.IgnoreCase to make it case-insensitive. Regex.Replace(argv, @"(Password=)[^\n]*", "$1********", RegexOptions.IgnoreCase);
3

This code replaces the password value by several "*" characters:

string argv = "-help=none\n-URL=(default)\n-password=look\n-uname=Khanna\n-p=100";
string result = Regex.Replace(argv, @"(password=)([\s\S]*?\n)",
    match => match.Groups[1].Value + new String('*', match.Groups[2].Value.Length - 1) + "\n");

You can also remove the new String() part and replace it by a string constant

2 Comments

Seems that it also cuts \n from \n-uname=K
This would work for me. But it is adding one more extra *. For look, it is creating *****

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.