首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在Windows上重新创建linux md5校验和

在Windows上重新创建linux md5校验和
EN

Stack Overflow用户
提问于 2021-09-14 16:29:08
回答 2查看 217关注 0票数 0

我正在对我们的linux机器执行一些非常简单的校验和,但现在我需要为我们的windows用户重新创建一些类似的校验和。要给我一个校验和,我只需运行:

代码语言:javascript
代码运行次数:0
运行
复制
md5sum *.txt | awk '{ print $1 }' | md5sum

我正在努力在Windows中重新创建它,无论是使用批处理文件还是Powershell。我得到的最接近的是:

代码语言:javascript
代码运行次数:0
运行
复制
Get-ChildItem $path -Filter *.txt | 
Foreach-Object {
   $hash =  Get-FileHash -Algorithm MD5 -Path ($path + "\" + $_) | Select -ExpandProperty "Hash"
   $hash = $hash.tolower()  #Get-FileHash returns checksums in uppercase, linux in lower case (!)
   Write-host $hash
}

这将向控制台输出与linux命令相同的每个文件的校验和结果,但是通过管道将其返回到Get-FileHash以获得与linux等效项匹配的单个输出,这让我难以理解。写入文件会让我遇到回车差异问题

以字符串的形式流回Get-FileHash不会返回相同的校验和:

代码语言:javascript
代码运行次数:0
运行
复制
$String = Get-FileHash -Algorithm MD5 -Path (Get-ChildItem -path $files -Recurse) | Select -ExpandProperty "Hash"
$stringAsStream = [System.IO.MemoryStream]::new()
$writer = [System.IO.StreamWriter]::new($stringAsStream)
$writer.write($stringAsStream)
Get-FileHash -Algorithm MD5 -InputStream $stringAsStream

我是不是过度设计了?我相信这不应该这么复杂!提亚

EN

回答 2

Stack Overflow用户

发布于 2021-09-14 17:02:51

您需要在从Get-FileHash返回的对象上引用.Hash属性。如果您想要一个类似于md5hash的视图,您也可以使用Select-Object来管理:

代码语言:javascript
代码运行次数:0
运行
复制
# Get filehashes in $path with similar output to md5sum
$fileHashes = Get-ChildItem $path -File | Get-FileHash -Algorithm MD5

# Once you have the hashes, you can reference the properties as follows
# .Algorithm is the hashing algo
# .Hash is the actual file hash
# .Path is the full path to the file
foreach( $hash in $fileHashes ){
  "$($hash.Algorithm):$($hash.Hash) ($($hash.Path))"
}

对于$path中的每个文件,上面的foreach循环将生成如下所示的行:

代码语言:javascript
代码运行次数:0
运行
复制
MD5:B4976887F256A26B59A9D97656BF2078 (C:\Users\username\dl\installer.msi)

根据您选择的散列算法和文件系统,算法、散列和文件名显然会有所不同。

票数 0
EN

Stack Overflow用户

发布于 2021-09-15 18:51:49

问题的关键在于细节:

  • (已知) Get-FileHash以大写形式返回校验和,而Linux md5sum以小写形式(!)返回;
  • FileSystem提供者的筛选器*.txt在PowerShell中不区分大小写,而在Linux中则取决于选项nocaseglob。如果设置为(shopt -s nocaseglob),则在执行文件名扩展时,Bash以不区分大小写的方式匹配文件名。否则,(shopt -u nocaseglob),文件名匹配是case-sensitive;
  • Order:Get-ChildItem输出根据Unicode collation algorithm排序,而在Linux中,*.txt过滤器按照LC_COLLATE类别(在我的系统上是LC_COLLATE="C.UTF-8")的顺序展开。

在以下(部分注释的)脚本中,三个# Test代码块演示了最终解决方案的调试步骤:

代码语言:javascript
代码运行次数:0
运行
复制
Function Get-StringHash {
    [OutputType([System.String])]
    param(
        # named or positional: a string
        [Parameter(Position=0)]
        [string]$InputObject
    )
    $stringAsStream = [System.IO.MemoryStream]::new()
    $writer = [System.IO.StreamWriter]::new($stringAsStream)
    $writer.write( $InputObject)
    $writer.Flush()
    $stringAsStream.Position = 0
    Get-FileHash -Algorithm MD5 -InputStream $stringAsStream |
        Select-Object -ExpandProperty Hash
    $writer.Close()
    $writer.Dispose()
    $stringAsStream.Close()
    $stringAsStream.Dispose()
}

function ConvertTo-Utf8String {
    [OutputType([System.String])]
    param(
        # named or positional: a string
        [Parameter(Position=0, Mandatory = $false)]
        [string]$InputObject = ''
    )
    begin {
        $InChars  = [char[]]$InputObject
        $InChLen  = $InChars.Count
        $AuxU_8 = [System.Collections.ArrayList]::new()
    }
    process {
        for ($ii= 0; $ii -lt $InChLen; $ii++) {
            if ( [char]::IsHighSurrogate( $InChars[$ii]) -and
                    ( 1 + $ii) -lt  $InChLen             -and
                    [char]::IsLowSurrogate( $InChars[1 + $ii]) ) {
                $s = [char]::ConvertFromUtf32(
                     [char]::ConvertToUtf32( $InChars[$ii], $InChars[1 + $ii]))
                $ii ++
            } else {
                $s = $InChars[$ii]
            }
            [void]$AuxU_8.Add( 
                ([System.Text.UTF32Encoding]::UTF8.GetBytes($s) | 
                    ForEach-Object { '{0:X2}' -f $_}) -join ''
            )
        }
    }
    end { $AuxU_8 -join '' }
}

# Set variables
$hashUbuntu = '5d944e44149fece685d3eb71fb94e71b'
$hashUbuntu   <# copied from 'Ubuntu 20.04 LTS' in Wsl2:
              cd `wslpath -a 'D:\\bat'`
              md5sum *.txt | awk '{ print $1 }' | md5sum | awk '{ print $1 }'
              <##>
$LF = [char]0x0A   # Line Feed (LF)
$path = 'D:\Bat'   # testing directory


$filenames = 'D:\bat\md5sum_Ubuntu_awk.lst'
<# obtained from 'Ubuntu 20.04 LTS' in Wsl2:
    cd `wslpath -a 'D:\\bat'`
    md5sum *.txt | awk '{ print $1 }' > md5sum_Ubuntu_awk.lst
    md5sum md5sum_Ubuntu_awk.lst | awk '{ print $1 }' # for reference
<##>

# Test #1: is `Get-FileHash` the same (beyond character case)?
$hashFile = Get-FileHash -Algorithm MD5 -Path $filenames |
                Select-Object -ExpandProperty Hash
$hashFile.ToLower() -ceq $hashUbuntu

# Test #2: is `$stringToHash` well-defined? is `Get-StringHash` the same?
$hashArray = Get-Content $filenames -Encoding UTF8
$stringToHash = ($hashArray -join $LF) + $LF
(Get-StringHash -InputObject $stringToHash) -eq $hashUbuntu 

# Test #3: another check: is `Get-StringHash` the same?
Push-Location -Path $path
$filesInBashOrder = bash.exe -c "ls -1 *.txt"
$hashArray = $filesInBashOrder |
    Foreach-Object {
        $hash = Get-FileHash -Algorithm MD5 -Path (
                        Join-Path -Path $path -ChildPath $_) |
                    Select-Object -ExpandProperty "Hash"
        $hash.tolower()
    }
$stringToHash = ($hashArray -join $LF) + $LF
(Get-StringHash -InputObject $stringToHash) -eq $hashUbuntu
Pop-Location

# Solution - ordinal order assuming `LC_COLLATE="C.UTF-8"` in Linux
Push-Location -Path $path
$hashArray = Get-ChildItem -Filter *.txt -Force -ErrorAction SilentlyContinue |
    Where-Object {$_.Name -clike "*.txt"} | # only if `shopt -u nocaseglob`
    Sort-Object -Property { (ConvertTo-Utf8String -InputObject $_.Name) } |
    Get-FileHash -Algorithm MD5 |
        Select-Object -ExpandProperty "Hash" |
    Foreach-Object {
        $_.ToLower()
    }
$stringToHash = ($hashArray -join $LF) + $LF
(Get-StringHash -InputObject $stringToHash).ToLower() -ceq $hashUbuntu
Pop-Location

输出(在278个文件上测试):.\SO\69181414.ps1

代码语言:javascript
代码运行次数:0
运行
复制
5d944e44149fece685d3eb71fb94e71b
True
True
True
True
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69181414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档