zoukankan      html  css  js  c++  java
  • C# SHA

    没有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
  • 相关阅读:
    [极客大挑战 2019]BuyFlag
    [极客大挑战 2019]BabySQL
    [网鼎杯 2018]Fakebook
    C语言学习笔记_内存数据和字符串
    剑指OFFER_数据流中的中位数
    剑指OFFER_滑动窗口的最大值
    剑指OFFER_矩阵中的路径
    C语言学习笔记_指针相关知识
    剑指OFFER_机器人的运动范围
    剑指OFFER_剪绳子
  • 原文地址:https://www.cnblogs.com/xzhblogs/p/5799070.html
Copyright © 2011-2022 走看看