码迷,mamicode.com
首页 > Windows程序 > 详细

C# SHA

时间:2016-08-23 14:47:37      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

没有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; }
              
        }

        } 
MySHA

 

C# SHA

标签:

原文地址:http://www.cnblogs.com/xzhblogs/p/5799070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!