How do I get the hash of current .exe?

被刻印的时光 ゝ 提交于 2019-11-30 15:37:54

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); 

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

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();
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!