.Net 프레임 워크에서 기존 SecureString 형식을 확장하기 위해 안전한 문자열 유형 (SecureStringV2)을 작성하기 시작했습니다. 이 새로운 유형은 기존 유형에 대한 몇 가지 기본 기능 (동등성 검사, 비교 등)을 추가하지만 SecureString 유형이 제공하는 보안을 유지합니다. 즉, 유형 사용 후 메모리에서 모든 것이 제거됩니다. 마샬 클래스와 해시 알고리즘을 사용하여 이러한 기능을 구현할 계획입니다. 이 일을 끝내고 올바르게 끝내는 방법에 대한 조언을 주시면 감사하겠습니다. 이 아이디어를 구현 한 아이디어에 문제가있는 사람이 있습니까? 고마워요.안전한 문자열 유형 구축시 고려 사항
업데이트 : 이것은 내 핵심 아이디어가 라이브러리의 핵심 클래스와 관련하여 지금까지 나를 이끌었습니다. 한 번 둘러보고 내 생각을 알려주세요.
/// <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
}
} 당신이 securestring의 혜택을 상실하기 시작에서 당신이 문자 [] 배열을 전달하는 경우
비교 방법에 '&'를 하나만 사용하고 있습니다. 그것들은 '&&'이어야합니다. – Doug