How to get the hash of the current .exe?

[SOLVED] : I copied the file and launched the hash in this copy.

I need an application to search for exe current MD5. I can get MD5 of any file. However, no matter what I do, I can not get FileStream to read the open EXE. I tried using FileOptions.Asynchronous, but that did not help.

EDIT: I guess I'm not very clear. I want my application to be able to read itself.

EDIT code:

private void GetMd5()
{
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

    FileInfo fi = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
    FileStream stream = File.Create(Process.GetCurrentProcess().MainModule.FileName, (int)fi.Length, FileOptions.Asynchronous);

    md5.ComputeHash(stream);

    stream.Close();

    string rtrn = "";
    for (int i = 0; i < md5.Hash.Length; i++)
    {
        rtrn += (md5.Hash[i].ToString("x2"));
    }
    MessageBox.Show(rtrn.ToUpper());
}
+3
source share
4 answers

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

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

, , . , FileInfo.Open Method (FileMode, FileAccess):

FileInfo fi = new FileInfo(path); 
FileStream stream = File.Open(path, FileMode.Open); 
+4

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

+1

, . .NET 4.5 , . , , , , .

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

, exe , -, .

, .

Link: 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;
    }
}

Each test seems to be outputting correctly. I recommend that anyone who sees this use this class or do something.

0
source

Source: https://habr.com/ru/post/1688575/


All Articles