0

私は次の機能を持っています:

public string GetRaumImageName()
    {
         var md5 = System.Security.Cryptography.MD5.Create();
        byte[] hash = md5.ComputeHash(Encoding.ASCII.GetBytes("Michael"));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("X2"));
        }
        return sb.ToString();
    }

これは、1つの値で正常に機能します。

ここで、複数の値を暗号化したい。私は何かを試しました:

public string GetRaumImageName()
    {
        var md5 = System.Security.Cryptography.MD5.Create();
        StringBuilder sb = new StringBuilder();
        byte[] hash = new byte[0];

        foreach (PanelView panelView in pv)
        {
            hash = md5.ComputeHash(Encoding.ASCII.GetBytes(panelView.Title));

        }

        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("X2"));

        }

        return sb.ToString();
    }

ただし、暗号化を取得する際のリストの最後の値のみ。リストにある複数の値を暗号化して返すにはどうすればよいですか?

4

2 に答える 2

2

すべてのハッシュをリストに追加してから、そのリストを返します。

public List<string> GetRaumImageName()
{
    var md5 = System.Security.Cryptography.MD5.Create();
    List<string> hashes = new List<string>();
    StringBuilder sb = new StringBuilder();
    byte[] hash = new byte[0];

    foreach (PanelView panelView in pv)
    {
        hash = md5.ComputeHash(Encoding.ASCII.GetBytes(panelView.Title));

        //clear sb
        sb.Remove(0, sb.Length);

        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("X2"));
        }
        hashes.Add(sb.ToString());
    }
    return hashes;
}
于 2013-02-07T12:34:21.007 に答える
1
    public IEnumerable<String> GetRaumImageName()
    {
        var md5 = System.Security.Cryptography.MD5.Create();

        byte[] hash = new byte[0];

        foreach (PanelView panelView in pv)
        {
            hash = md5.ComputeHash(Encoding.ASCII.GetBytes(panelView.Title));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));

            }
            yield return sb.ToString();
        }          
    }

Valuesこれにより、必要なものがすべて返されます。IEnumerable<String>

典型的な使用法

var values = GetRaumImageName();
foreach(value in values)
{
    // use the 'value'
}
于 2013-02-07T12:29:44.383 に答える