views:

142

answers:

2
+2  Q: 

Using SecureString

Can this be simplified to a one liner? Feel free to completely rewrite it as long as secureString gets initialized properly.

SecureString secureString = new SecureString ();
foreach (char c in "fizzbuzz".ToCharArray())
{
    secureString.AppendChar (c);
}
+4  A: 

Apart from using unsafe code and a char*, there isn't a (much) better way.

The point here is not to copy SecureString contents to/from normal string ("fizzbuzz" is a securityleak).

Henk Holterman
Beat me to it -- +1. Plus the additional changes you need to make to allow for unsafe code negates any "savings" on lines of code.
Austin Salonen
Don't most passwords originate in most software as strings and then need to be converted to a SecureString? Not sure what you mean by "not to copy SecureString contents from normal string". In normal circumstances that would be string password. "fizzbuzz" is just a homage.
Todd Smith
Yes, and that greatly reduces the usability of SecureString.
Henk Holterman
SecureString is a property of ProcessStartInfo and is needed for Process.Start(). Blame MS not the messenger :)
Todd Smith
If you're collecting a SecureString from keystrokes, you don't actually have an original string. This, I believe, was the original intent of SecureString.
Doug
+2  A: 

You could use Linq:

"fizzbuzz".ToCharArray ().ToList ().ForEach ( p => secureString.AppendChar ( p ) );

-sa

Sascha
+1 Actually, I think that´s the same that @Tod proposed, but with less lines.
Javier Morillo
I guess I can throw this into an extension method to get what I'm after: processInfo.Password = new SecureSring ().FromString ("fizzbuzz")
Todd Smith