How do I get the hash of current .exe?

前端 未结 4 381
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 05:45

[SOLVED]: I copied the file and ran the hasher on that copy.

I need my app to find the EXE\'s current MD5. I can get the MD5 of any file. However, n

相关标签:
4条回答
  • 2021-01-03 06:37

    Bit late to the party, but was recently trying to do this myself. In .NET 4.5 the following works quite nicely without the need for making a temporary copy. As said, if you can read the file to make a copy of it, you can read the file to generate a hash for it.

    private string GetMD5()
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        System.IO.FileStream stream = new System.IO.FileStream(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    
        md5.ComputeHash(stream);
    
        stream.Close();
    
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < md5.Hash.Length; i++)
            sb.Append(md5.Hash[i].ToString("x2"));
    
        return sb.ToString().ToUpperInvariant();
    }
    
    0 讨论(0)
  • 2021-01-03 06:41

    The File.Create Method (String, Int32, FileOptions, FileSecurity):

    Creates or overwrites the specified file with the specified buffer size, file options, and file security.

    I'm fairly sure that's not what you intended to do. Presumably you want FileInfo.Open Method (FileMode, FileAccess):

    FileInfo fi = new FileInfo(path); 
    FileStream stream = File.Open(path, FileMode.Open); 
    
    0 讨论(0)
  • 2021-01-03 06:42

    Change: FileStream stream = File.Create(path, (int)fi.Length, FileOptions.Asynchronous); to FileStream stream = File.Open(path, FileMode.Open);

    0 讨论(0)
  • 2021-01-03 06:52

    Since I've tried these answers and found that none of them change every edit to the exe, or change at all, I found something that actually does work.

    I did not edit any code here, all of this was from the referred page below.

    Reference: http://www.vcskicks.com/self-hashing.php

    internal static class ExecutingHash
    {
        public static string GetExecutingFileHash()
        {
            return MD5(GetSelfBytes());
        }
    
        private static string MD5(byte[] input)
        {
            return MD5(ASCIIEncoding.ASCII.GetString(input));
        }
    
        private static string MD5(string input)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    
            byte[] originalBytes = ASCIIEncoding.Default.GetBytes(input);
            byte[] encodedBytes = md5.ComputeHash(originalBytes);
    
            return BitConverter.ToString(encodedBytes).Replace("-", "");
        }
    
        private static byte[] GetSelfBytes()
        {
            string path = Application.ExecutablePath;
    
            FileStream running = File.OpenRead(path);
    
            byte[] exeBytes = new byte[running.Length];
            running.Read(exeBytes, 0, exeBytes.Length);
    
            running.Close();
    
            return exeBytes;
        }
    }
    

    Every test seems to output properly. I'd recommend to anyone seeing this to use this class or make something of it.

    0 讨论(0)
提交回复
热议问题