1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 00:16:27 +00:00

Added locking of SecureString, wasn't thread safe

This commit is contained in:
JKorf 2018-08-16 08:24:54 +02:00
parent 5328695f5f
commit 00456ea6d2

View File

@ -31,27 +31,30 @@ namespace CryptoExchange.Net
public static string GetString(this SecureString source) public static string GetString(this SecureString source)
{ {
string result = null; lock (source)
int length = source.Length;
IntPtr pointer = IntPtr.Zero;
char[] chars = new char[length];
try
{ {
pointer = Marshal.SecureStringToBSTR(source); string result = null;
Marshal.Copy(pointer, chars, 0, length); int length = source.Length;
IntPtr pointer = IntPtr.Zero;
char[] chars = new char[length];
result = string.Join("", chars); try
}
finally
{
if (pointer != IntPtr.Zero)
{ {
Marshal.ZeroFreeBSTR(pointer); pointer = Marshal.SecureStringToBSTR(source);
} Marshal.Copy(pointer, chars, 0, length);
}
return result; result = string.Join("", chars);
}
finally
{
if (pointer != IntPtr.Zero)
{
Marshal.ZeroFreeBSTR(pointer);
}
}
return result;
}
} }
} }
} }