我有以下方法:
@H_301_2@public static string Sha256Hash(string input) {
if(String.IsNullOrEmpty(input)) return String.Empty;
using(HashAlgorithm algorithm = new SHA256CryptoServiceProvider()) {
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = algorithm.ComputeHash(inputBytes);
return BitConverter.ToString(hashBytes).Replace("-",String.Empty);
}
}
有没有办法让它异步?我希望使用async和await关键字,但HashAlgorithm类不为此提供任何异步支持.
另一种方法是将所有逻辑封装在:
@H_301_2@public static async string Sha256Hash(string input) { return await Task.Run(() => { //Hashing here... }); }但这似乎并不干净,我不确定它是否是一种异步执行操作的正确(或有效)方式.
我该怎么做才能做到这一点?