Home > database >  Generating MD5 Checksum value of text file
Generating MD5 Checksum value of text file

Time:11-07

I wanted to create a checksum of a file I currently store locally. Within the files contents I will need to checksum the body of the file only excluding the first and last line (header and footer). The headers and footers always begin with >>

I have currently implemented code in c# to generate the checksum but that generates it for all the files contents. I currently have two options either generate this within c# code or generate it using command prompt on windows.

My current c# code looks something like this:

            string CalculateMD5(string fileLocation)
            {
                using (var md5 = MD5.Create())
                {
                    using (var stream = File.OpenRead(fileLocation))
                    {
                        var hash = md5.ComputeHash(stream);
                        return BitConverter.ToString(hash).Replace("-", "");
                    }
                }
            }

Also I have tried using this cmd command: Certutil -hashfile filename.txt MD5

Again this generates the MD5 value for the whole file which is not the required output.

p.s. I did try removing first and last line using c# and then generating the md5 hash, however the value seemed to differ from what it should be.

Any and all suggestions welcome :)

Thanks

CodePudding user response:

just quickly throwing something together, wouldn't something like this solve your problem?

private string CalculateMD5(string path) {
    using var md5 = MD5.Create();

    var txt = string.Join('\n', File.ReadAllLines(path)[1..^1]);

    var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(txt));

    var result = BitConverter.ToString(hash).Replace("-", "");

    return result;
}

joining with '\n' to add the missing newlines when reading the file as lines, to avoid that information being lost, the range operator is a newer C# feature, but can easily be done in the old fashioned way, if your solution requires it

CodePudding user response:

If you are on a supported Windows system, PowerShell is already installed and available unless your organization has taken steps to restrict it.

Place both files, Get-Md5sum.bat and Get-Md5sum.ps1, into the same directory that is in your PATH variable.

In your case, you would produce a temporary file without the header/footer records, then run Get-Md5sum on the temporary file.

PS C:\src\t> Get-Content .\Get-Md5sum.bat
@powershell -NoLogo -NoProfile -Command "%~dp0Get-Md5sum.ps1 -Path "%~1""

PS C:\src\t> Get-Content .\Get-Md5sum.ps1
[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true)]
    [string]$Path
)
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
[System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($Path)))

PS C:\src\t> .\Get-Md5sum.bat "C:\src\t\test.txt"
FD-99-05-E1-CA-CE-FA-81-02-D1-C7-D3-35-19-E3-C3

PS C:\src\t> .\Get-Md5sum.ps1 -Path "C:\src\t\test.txt"
FD-99-05-E1-CA-CE-FA-81-02-D1-C7-D3-35-19-E3-C3

PS C:\src\t> .\Get-Md5sum.ps1 "C:\src\t\test.txt"
FD-99-05-E1-CA-CE-FA-81-02-D1-C7-D3-35-19-E3-C3

The result can have the HYPHEN-MINUS characters removed and/or converted to lowercase if you prefer.

PS C:\src\t> (.\Get-Md5sum.ps1 -Path "C:\src\t\test.txt") -replace '-',''
FD9905E1CACEFA8102D1C7D33519E3C3

PS C:\src\t> (.\Get-Md5sum.ps1 -Path "C:\src\t\test.txt").ToLower()
fd-99-05-e1-ca-ce-fa-81-02-d1-c7-d3-35-19-e3-c3

PS C:\src\t> (.\Get-Md5sum.ps1 -Path "C:\src\t\test.txt").ToLower() -replace '-',''
fd9905e1cacefa8102d1c7d33519e3c3
  • Related