1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +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)
{
string result = null;
int length = source.Length;
IntPtr pointer = IntPtr.Zero;
char[] chars = new char[length];
try
lock (source)
{
pointer = Marshal.SecureStringToBSTR(source);
Marshal.Copy(pointer, chars, 0, length);
string result = null;
int length = source.Length;
IntPtr pointer = IntPtr.Zero;
char[] chars = new char[length];
result = string.Join("", chars);
}
finally
{
if (pointer != IntPtr.Zero)
try
{
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;
}
}
}
}