当前位置: 首页 > news >正文

C#问题解决方案 --- 生成软件hash,生成文件hash

生成软件hash值:

private string GetEXEHashString()
{
    //获得软件哈希值
    Process currProcess = Process.GetCurrentProcess();
    string filePath = currProcess.MainModule.FileName;
    string hashEXE = string.Empty;
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        //  hash 算法
        MD5 algorithm = MD5.Create();

        byte[] hashData = algorithm.ComputeHash(fs);
        hashEXE = ByteArrayToHexString(hashData);
        fs.Close();
    }
    Console.WriteLine(hashEXE.ToString());
    Info("GetEXEHashString :hashEXE = " + hashEXE.ToString());
    return hashEXE.ToString();
}

private static string ByteArrayToHexString(byte[] bytes)
{
    int length = bytes.Length;

    StringBuilder sb = new StringBuilder();

    foreach (byte data in bytes)
    {
        sb.Append(data.ToString("x2"));
    }

    return sb.ToString();
}

生成文件hash值

 public static String ComputeMD5(String fileName)
 {
     String hashMD5 = String.Empty;
     //检查文件是否存在,如果文件存在则进行计算,否则返回空值
     if (System.IO.File.Exists(fileName))
     {

         using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
         {
             //计算文件的MD5值
             System.Security.Cryptography.MD5 calculator = System.Security.Cryptography.MD5.Create();

             Byte[] buffer = calculator.ComputeHash(fs);
             calculator.Clear();
             //将字节数组转换成十六进制的字符串形式
             StringBuilder stringBuilder = new StringBuilder();
             for (int i = 0; i < buffer.Length; i++)
             {
                 stringBuilder.Append(buffer[i].ToString("x2"));
             }
             hashMD5 = stringBuilder.ToString();
         }//关闭文件流
     }//结束计算
     return hashMD5;
 }//ComputeMD5

相关文章:

  • git merge -s ours ...的使用方法
  • 数据安全_笔记系列10:数据分类分级与保护策略详解
  • threejs:射线拾取封装
  • 计算机毕业设计 ——jspssm518Springboot 的影视影院订票选座管理系统
  • unity使用PICO Neo3开发,XR环境配置
  • 异常(2)
  • Java高频面试之SE-23
  • 27.[前端开发-JavaScript基础]Day04-函数基本使用-递归-变量作用域-函数式编程
  • 结构型模式 - 代理模式 (Proxy Pattern)
  • 利用python进行数据分析(重点、易忘点)---第八章数据规整:聚合、合并和重塑
  • Linux查看和处理文件内容
  • 【网络编程】网络套接字和使用案例
  • 数学与计算生物学:生物系统的数学建模
  • vs code默认主题修改配置
  • 大白话JavaScript如何深拷贝一个对象或数组?JSON.parse (JSON.stringify ()) 这种方法有什么局限性?
  • ss命令——查看网络连接信息
  • vue+qrcode2批量生成二维码
  • 【Python爬虫(74)】用Python爬虫解锁法律条文数据的宝库
  • C语言 --- 经典习题1
  • 使用 C# 以api的形式调用 DeepSeek
  • wordpress速度加快/宁波优化网站哪家好
  • 手机微网站建设案例及报告/郑州网络推广专业公司
  • 迁西个人网页设计制作软件/苏州关键词seo排名
  • 拱墅区哪里有网站建设/微信seo是什么意思
  • 工业设计公司简介/行者seo无敌
  • 网页界面设计用什么软件/长沙seo优化排名