forked from github/VisualStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureStringHelper.cs
More file actions
47 lines (45 loc) · 1.23 KB
/
SecureStringHelper.cs
File metadata and controls
47 lines (45 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace GitHub.Authentication.CredentialManagement
{
[SuppressUnmanagedCodeSecurity]
internal static class SecureStringHelper
{
// Methods
internal static SecureString CreateSecureString(string plainString)
{
var str = new SecureString();
if (!string.IsNullOrEmpty(plainString))
{
foreach (char c in plainString)
{
str.AppendChar(c);
}
}
return str;
}
internal static string CreateString(SecureString secureString)
{
string str;
var zero = IntPtr.Zero;
if ((secureString == null) || (secureString.Length == 0))
{
return string.Empty;
}
try
{
zero = Marshal.SecureStringToBSTR(secureString);
str = Marshal.PtrToStringBSTR(zero);
}
finally
{
if (zero != IntPtr.Zero)
{
Marshal.ZeroFreeBSTR(zero);
}
}
return str;
}
}
}