0

.Net フレームワークで既存の SecureString 型を拡張するために、SecureStringV2と呼ぶセキュアな文字列型の構築を開始しました。この新しい型は、いくつかの基本的な機能 (等しいかどうかのチェック、比較など) を既存の型に追加しますが、型の使用後にメモリからすべてを消去する 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
}

}

4

3 に答える 3

1

SecureString は双方向の暗号化を使用することを忘れないでください。あなたのクラスには暗号化が見られません。

于 2010-08-25T16:28:06.957 に答える
1

1 つだけ簡単なメモ - ハッシュの等価性とデータの等価性を取り違える問題について尋ねています - それらが異なる可能性は低いことを確認してください。数年前に同じ間違いを犯したカスタムハッシュマップの実装を修正してください。これについては私を信頼してください;))。

于 2009-03-12T18:58:15.743 に答える
0

char[] 配列を渡すと、セキュアストリングの利点が失われ始めます。char[] 配列は、クリアされる前にガベージ コレクターによってメモリ内にコピーされる可能性があります。

于 2009-03-12T17:30:29.277 に答える