标签:
没有sha224
要注意每次计算完之后要把文件释放掉,再重新打开
否则会计算出错
static class MySHA { public static string[] GetSHAHash(string Message) { string[] sha = new string[4]; try { SHA1 sha1 = new SHA1CryptoServiceProvider(); SHA256 sha256 = new SHA256CryptoServiceProvider(); SHA384 sha384 = new SHA384CryptoServiceProvider(); SHA512 sha512 = new SHA512CryptoServiceProvider(); byte[] sha_in = UTF8Encoding.Default.GetBytes(Message); byte[] sha1_out = sha1.ComputeHash(sha_in); byte[] sha256_out = sha256.ComputeHash(sha_in); byte[] sha384_out = sha384.ComputeHash(sha_in); byte[] sha512_out = sha512.ComputeHash(sha_in); sha[0] = BitConverter.ToString(sha1_out).Replace("-", ""); sha[1] = BitConverter.ToString(sha256_out).Replace("-", ""); sha[2] = BitConverter.ToString(sha384_out).Replace("-", ""); sha[3] = BitConverter.ToString(sha512_out).Replace("-", ""); return sha; } catch { MessageBox.Show("校验失败");return sha; } } public static string[] GetSHAHashFromFile(string fileName) { string[] sha = new string[4]; try { SHA1 sha1 = new SHA1CryptoServiceProvider(); SHA256 sha256 = new SHA256CryptoServiceProvider(); SHA384 sha384 = new SHA384CryptoServiceProvider(); SHA512 sha512 = new SHA512CryptoServiceProvider(); FileStream file = new FileStream(fileName, FileMode.Open); byte[] sha1_out = sha1.ComputeHash(file); file.Close(); file = new FileStream(fileName, FileMode.Open); byte[] sha256_out = sha256.ComputeHash(file); file.Close(); file = new FileStream(fileName, FileMode.Open); byte[] sha384_out = sha384.ComputeHash(file); file.Close(); file = new FileStream(fileName, FileMode.Open); byte[] sha512_out = sha512.ComputeHash(file); file.Close(); sha[0] = BitConverter.ToString(sha1_out).Replace("-", ""); sha[1] = BitConverter.ToString(sha256_out).Replace("-", ""); sha[2] = BitConverter.ToString(sha384_out).Replace("-", ""); sha[3] = BitConverter.ToString(sha512_out).Replace("-", ""); return sha; } catch { MessageBox.Show("校验失败"); return sha; } } }
标签:
原文地址:http://www.cnblogs.com/xzhblogs/p/5799070.html