我已经开始构建一个安全字符串类型——我称之为SecureStringV2——来扩展 .Net 框架中现有的 SecureString 类型。这种新类型将为现有类型添加一些基本功能(检查相等性、比较等),但仍保持 SecureString 类型提供的安全性,即在类型使用后清除内存中的所有内容。我计划使用 Marshal 类和哈希算法来实现这些功能。关于如何完成这项工作并正确完成的指针将不胜感激。你们中是否有人认为我的实施想法有任何问题?谢谢 :)
更新:关于图书馆的核心类,这是我的想法到目前为止引导我的地方。看看,让我知道你的想法。
/// <summary>
/// This class is extension of the SecureString Class in the .Net framework.
/// It provides checks for equality of multiple SStringV2 instances and maintains
/// the security provided by the SecureString Class
/// </summary>
public class SStringV2 : IEquatable<SStringV2> , IDisposable
{
private SecureString secureString = new SecureString();
private Byte[] sStringBytes;
private String hash = string.Empty;
/// <summary>
/// SStringV2 constructor
/// </summary>
/// <param name="confidentialData"></param>
public SStringV2(ref Char[] confidentialData)
{
GCHandle charArrayHandle = GCHandle.Alloc(confidentialData, GCHandleType.Pinned);
// The unmanaged string splices a zero byte inbetween every two bytes
//and at its end doubling the total number of bytes
sStringBytes = new Byte[confidentialData.Length*2];
try
{
for (int index = 0; index < confidentialData.Length; ++index)
{
secureString.AppendChar(confidentialData[index]);
}
}
finally
{
ZeroOutSequence.ZeroOutArray(ref confidentialData);
charArrayHandle.Free();
}
}
/// <summary>
/// Computes the hash value of the secured string
/// </summary>
private void GenerateHash()
{
IntPtr unmanagedRef = Marshal.SecureStringToBSTR(secureString);
GCHandle byteArrayHandle = GCHandle.Alloc(sStringBytes, GCHandleType.Pinned);
Marshal.Copy(unmanagedRef, sStringBytes, 0, sStringBytes.Length);
SHA256Managed SHA256 = new SHA256Managed();
try
{
hash = Convert.ToBase64String(SHA256.ComputeHash(this.sStringBytes));
}
finally
{
SHA256.Clear();
ZeroOutSequence.ZeroOutArray(ref sStringBytes);
byteArrayHandle.Free();
Marshal.ZeroFreeBSTR(unmanagedRef);
}
}
#region IEquatable<SStringV2> Members
public bool Equals(SStringV2 other)
{
if ((this.hash == string.Empty) & ( other.hash == string.Empty))
{
this.GenerateHash();
other.GenerateHash();
}
else if ((this.hash == string.Empty) & !(other.hash == string.Empty))
{
this.GenerateHash();
}
else if (!(this.hash == string.Empty) & (other.hash == string.Empty))
{
other.GenerateHash();
}
if (this.hash.Equals(other.hash))
{
return true;
}
return false;
}
#endregion
#region IDisposable Members
public void Dispose()
{
secureString.Dispose();
hash = string.Empty;
GC.SuppressFinalize(this);
}
#endregion
}
}