Re: Removing the password from Session.Output
There no other machanism, sorry.
To escape special characters in password, use System.Uri.EscapeDataString.
Ah! I couldn't find that. Thanks!
There no other machanism, sorry.
To escape special characters in password, use System.Uri.EscapeDataString.
private static void DisplaySessionException(WinSCP.SessionOptions so, WinSCP.Session sess, Exception ex)
{
Console.WriteLine("Exception encountered: {0}", ex.Message);
foreach (string s in sess.Output)
{
Console.WriteLine("\t{0}", FilterPassword(so, s));
}
}
private static string FilterPassword(WinSCP.SessionOptions so, string s)
{
if (!s.Contains("winscp> open"))
return s;
string encodedPassword = PartialEncode(so.Password);
return s.Replace(encodedPassword, new string('*', so.Password.Length));
}
private static string PartialEncode(string asciiString)
{
string convertedString = "";
foreach (char c in asciiString)
{
switch (c) {
case '`': case '@': case '#': case '$': case '%':
case '^': case '&': case ' ': case '+': case '/':
case '{': case '}': case ':': case '"': case '<':
case '>': case '?': case ',': case ';': case '[':
case ']': case '\\': case '=':
case '|': // Forgot this
convertedString += String.Format("%{0:X2}", (uint)Convert.ToUInt32(((int)c).ToString()));
break;
default:
convertedString += c;
break;
}
}
return convertedString;
}
private static void DisplaySessionException(WinSCP.SessionOptions so, WinSCP.Session sess, Exception ex)
{
Console.WriteLine("Exception encountered: {0}", ex.Message);
foreach (string s in sess.Output)
{
Console.WriteLine("\t{0}", FilterPassword(so, s));
}
}
private static string FilterPassword(WinSCP.SessionOptions so, string s)
{
if (!s.Contains("winscp>"))
return s;
string encodedPassword = so.Password
.Replace("%", "%25")
.Replace(" ", "%20")
.Replace(" ", "%20")
.Replace("+", "%2B")
.Replace("/", "%2F")
.Replace("@", "%40")
// ...
;
return s.Replace(encodedPassword, new string('*', so.Password.Length));
}