-
Notifications
You must be signed in to change notification settings - Fork 809
Expand file tree
/
Copy pathSecureStringHelper.cs
More file actions
37 lines (30 loc) · 913 Bytes
/
SecureStringHelper.cs
File metadata and controls
37 lines (30 loc) · 913 Bytes
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
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace NETworkManager.Utilities;
public static class SecureStringHelper
{
public static string ConvertToString(SecureString secureString)
{
var valuePtr = IntPtr.Zero;
try
{
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(secureString);
return Marshal.PtrToStringUni(valuePtr);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
}
}
public static SecureString ConvertToSecureString(string clearText)
{
if (clearText == null)
throw new ArgumentNullException(nameof(clearText));
var securePassword = new SecureString();
foreach (var c in clearText)
securePassword.AppendChar(c);
securePassword.MakeReadOnly();
return securePassword;
}
}