1

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

unknown
  • 31
  • 5
  • 2
    If you remove the header and footer from the file, then hashed it with this method and you don't get the value you want. then you are lost. Your expectations are wrong, and this question is missing information – TheGeneral Nov 05 '21 at 09:37
  • What is the purpose of calculating a hash value for only part of a file? Are you trying to determine if the same data is in a file with differing header/footer information? – lit Nov 05 '21 at 18:05
  • @lit it's verify the contents of the file i.e. the body, the header and footer are of no importance. – unknown Nov 06 '21 at 20:44
  • You could create a temporary file without the header/footer records and use https://stackoverflow.com/a/10521162/447901 to calculate an MD5 hash value. – lit Nov 06 '21 at 23:49

2 Answers2

0

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

A Sad Shoe
  • 134
  • 6
  • Thanks for the reply. It seems as though I am using an older version of c# language as it doesn't support range function – unknown Nov 06 '21 at 20:46
  • In that case you could do `Array.Copy(sourceArrayFromReadLines, 1, destinationArray, 0, sourceArrayFromReadLines.Length - 2)` and then use `string.Join` on them instead, `sourceArrayFromReadLines` being a variable assigned with the value of File.ReadAllLines(path), and the destination array being a new array `string[sourceArrayFromReadLines.Length - 2]` – A Sad Shoe Nov 08 '21 at 12:30
0

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
lit
  • 14,456
  • 10
  • 65
  • 119