简体   繁体   English

生成文本文件的 MD5 校验和值

[英]Generating MD5 Checksum value of text file

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.我目前已经在 c# 中实现了代码来生成校验和,但这会为所有文件内容生成它。 I currently have two options either generate this within c# code or generate it using command prompt on windows.我目前有两个选项,要么在 c# 代码中生成它,要么在 Windows 上使用命令提示符生成它。

My current c# code looks something like this:我当前的 C# 代码如下所示:

            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我也试过使用这个 cmd 命令:Certutil -hashfile filename.txt MD5

Again this generates the MD5 value for the whole file which is not the required output.这再次为整个文件生成 MD5 值,这不是所需的输出。

ps 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. ps 我确实尝试使用 c# 删除第一行和最后一行,然后生成 md5 哈希,但是该值似乎与应有的不同。

Any and all suggestions welcome :)欢迎任何和所有建议:)

Thanks谢谢

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加入 '\\n' 以在将文件作为行读取时添加缺少的换行符,以避免丢失该信息,范围运算符是较新的 C# 功能,但如果您的解决方案需要它,可以轻松地以老式方式完成

在 linux 操作系统中,您可以输入“md5sum”和文件名

If you are on a supported Windows system, PowerShell is already installed and available unless your organization has taken steps to restrict it.如果您使用的是受支持的 Windows 系统,则 PowerShell 已安装且可用,除非您的组织已采取措施对其进行限制。

Place both files, Get-Md5sum.bat and Get-Md5sum.ps1, into the same directory that is in your PATH variable.将 Get-Md5sum.bat 和 Get-Md5sum.ps1 这两个文件放入 PATH 变量中的同一目录中。

In your case, you would produce a temporary file without the header/footer records, then run Get-Md5sum on the temporary file.在您的情况下,您将生成一个没有页眉/页脚记录的临时文件,然后在临时文件上运行 Get-Md5sum。

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.如果您愿意,结果可以删除 HYPHEN-MINUS 字符和/或转换为小写。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM