我得到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