DEV Community

Pavel Morava
Pavel Morava

Posted on

How to compute the hash of a file in C# script?

Hi everyone,

recently, I decided to dive into C# once again. To make myself as comfortable as possible I utilized its scripting ability.

A few weeks ago, my friend asked me to make a program computing hashes of files. Just for fun, I wrote a simple script that compute and write the MD5 hash of a file, given you use a command like this:

dotnet-script your_script.csx your_file_to_hash
Enter fullscreen mode Exit fullscreen mode

The script is as follows:

using System.Security.Cryptography;

    new StringBuilder()
            .AppendJoin("", HashAlgorithm.Create("md5")
                    .Select(x => $"{x:X2}")

Enter fullscreen mode Exit fullscreen mode

If you have any questions, feel free to ask.

Discussion (0)