我得到ObjectDisposedException:安全句柄已關閉。ObjectDisposedException當使用MD5 ComputeHash
這是我的代碼:
我想創建一個接口和實現類,使我得到一個字符串,附加到它已知的關鍵,計算MD5哈希此字符串和關鍵,返回所計算的哈希:
public interface ISignService
{
string GetSignature(string str);
}
public class SignService : ISignService
{
private readonly ISignSettings _signSettings;
private readonly HashAlgorithm _hashAlgo;
public SignService(ISignSettings signSettings)
{
_signSettings = signSettings;
_hashAlgo = MD5.Create();
}
public string GetSignature(string str)
{
var strWithKey = str + _signSettings.EncryptionKey;
var hashed = _hashAlgo.ComputeHash(Encoding.UTF8.GetBytes(strWithKey));
return hashed.ToHexString();
}
}
感謝
哪一行代碼會引發異常? – ken2k
是'hashed.ToHexString()'的擴展方法嗎?我粘貼你的代碼和方法不存在... – Laoujin
嗨,這是一種擴展方法。引發的行是:var hashed = _hashAlgo.ComputeHash(Encoding.UTF8.GetBytes(strWithKey)); – user1625867