Skip to content

Commit

Permalink
(chocolateyGH-8) Convert to SecureString
Browse files Browse the repository at this point in the history
Add extension for converting a string to a SecureString. This should
return an empty securestring if the value passed in is null or empty.
  • Loading branch information
ferventcoder committed Jan 1, 2016
1 parent a17fad3 commit d062370
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/chocolatey/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace chocolatey
{
using System;
using System.Globalization;
using System.Security;
using System.Text.RegularExpressions;
using infrastructure.app;
using infrastructure.logging;
Expand Down Expand Up @@ -83,6 +84,26 @@ public static string to_string(this string input)
return input;
}

/// <summary>
/// Takes a string and returns a secure string
/// </summary>
/// <param name="input">The input.</param>
/// <returns></returns>
public static SecureString to_secure_string(this string input)
{
var secureString = new SecureString();

if (string.IsNullOrWhiteSpace(input)) return secureString;

foreach (char character in input)
{
secureString.AppendChar(character);
}

return secureString;
}


private static readonly Regex _spacePattern = new Regex(@"\s", RegexOptions.Compiled);

/// <summary>
Expand Down

0 comments on commit d062370

Please sign in to comment.