PowerShell 7.0如何计算按块读取的大文件的哈希和

科斯蒂安

该脚本应复制文件并计算它们的哈希值总和。我的目标是使该函数读取文件而不是3(read_for_copy + read_for_hash + read_for_another_copy)一次,以最大程度地减少网络负载。因此,我尝试读取一块文件,然后计算md5哈希和并将文件写出到几个位置。文件大小可能从100 MB到2 TB,甚至更大。此时无需检查文件身份,只需计算初始文件的哈希值即可。

而且我对计算散列总和感到困惑:

    $ifile = "C:\Users\User\Desktop\inputfile"
    $ofile = "C:\Users\User\Desktop\outputfile_1"
    $ofile2 = "C:\Users\User\Desktop\outputfile_2"
    
    $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    $bufferSize = 10mb
    $stream = [System.IO.File]::OpenRead($ifile)
    $makenew = [System.IO.File]::OpenWrite($ofile)
    $makenew2 = [System.IO.File]::OpenWrite($ofile2)
    $buffer = new-object Byte[] $bufferSize
    
    while ( $stream.Position -lt $stream.Length ) {
       
     $bytesRead = $stream.Read($buffer, 0, $bufferSize)
     $makenew.Write($buffer, 0, $bytesread) 
     $makenew2.Write($buffer, 0, $bytesread) 
    
     # I am stuck here
     $hash = [System.BitConverter]::ToString($md5.ComputeHash($buffer)) -replace "-",""      
            
            }
    
    $stream.Close()
    $makenew.Close()
    $makenew2.Close()

如何收集数据块以计算整个文件的哈希?

还有一个额外的问题:是否可以在并行模式下计算哈希并写出数据?特别考虑到workflow {parallel{}}PS版本6不支持的内容?

非常感谢

科斯蒂安

最终上市

$ifile = "C:\Users\User\Desktop\inputfile"
$ofile = "C:\Users\User\Desktop\outputfile_1"
$ofile2 = "C:\Users\User\Desktop\outputfile_2"

$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$bufferSize = 1mb
$stream = [System.IO.File]::OpenRead($ifile)
$makenew = [System.IO.File]::OpenWrite($ofile)
$makenew2 = [System.IO.File]::OpenWrite($ofile2)
$buffer = new-object Byte[] $bufferSize

while ( $stream.Position -lt $stream.Length ) 
{
     $bytesRead = $stream.Read($buffer, 0, $bufferSize)
     $makenew.Write($buffer, 0, $bytesread) 
     $makenew2.Write($buffer, 0, $bytesread) 
    
     $hash = $md5.TransformBlock($buffer, 0 , $bytesRead, $null , 0)  
} 

$md5.TransformFinalBlock([byte[]]::new(0), 0, 0)
$hash = [BitConverter]::ToString($md5.Hash).Replace('-','')      
$hash
$stream.Flush()
$stream.Close()
$makenew.Flush()
$makenew.Close()
$makenew2.Flush()
$makenew2.Close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

.bat文件无法运行Powershell 7

来自分类Dev

Windows 7上的Powershell如何区分两个文件?

来自分类Dev

使用powershell / 7za.exe提取多个zip文件?

来自分类Dev

Windows 7上Powershell配置文件的位置

来自分类Dev

如何让Winrm默认使用Powershell 7进行远程会话

来自分类Dev

如何使用Powershell Core 7解析HTML表?

来自分类Dev

如何在PowerShell 7中使用AzureRm模块?

来自分类Dev

如何在Windows 7中设置PowerShell默认窗口大小?

来自分类Dev

如何使用PowerShell在Windows 7上检查IE版本?

来自分类Dev

使用PowerShell读取大文件并删除回车

来自分类Dev

如何在Azure Functions 3.x PowerShell 7中调用Azure PowerShell模块命令?

来自分类Dev

如何从命令提示符执行Powershell脚本AS Powershell 7

来自分类Dev

Powershell无法读取哈希

来自分类Dev

如何通过Powershell运行7zip来解压缩.rar文件夹?

来自分类Dev

如何从文件读取Powershell表

来自分类Dev

在Windows 7(cmd或PowerShell)上的子文件夹中展平文件

来自分类Dev

在Windows Server 2012上使用PowerShell删除7天以上的文件

来自分类Dev

PowerShell-上次保存的文件-希望缩小到最近7天

来自分类Dev

无法使用Powershell生成的随机密码打开7z文件

来自分类Dev

Powershell脚本可返回最近7天内未创建/修改的Zip文件的目录

来自分类Dev

我的 Windows 7 机器上 powershell 配置文件的位置不一致

来自分类Dev

Windows 7和Server 2008中的Powershell-有区别吗?

来自分类Dev

Powershell SOAP服务-找不到..和参数计数为“ 7”的重载

来自分类Dev

Powershell 7-集内容和外文件将回车符(CR)添加到我的文件中

来自分类Dev

使用PowerShell下载大文件

来自分类Dev

如何使用Powershell在远程数据库上运行Entity Framework 7迁移?

来自分类Dev

如何使PowerShell 4 cmdlet(如Test-NetConnection)在Windows 7上运行?

来自分类Dev

安装3.5后如何从win7 powershell启动python 2.7?

来自分类Dev

如何在PowerShell 7中对TTS使用Windows System.Speech(或有替代方法)

Related 相关文章

  1. 1

    .bat文件无法运行Powershell 7

  2. 2

    Windows 7上的Powershell如何区分两个文件?

  3. 3

    使用powershell / 7za.exe提取多个zip文件?

  4. 4

    Windows 7上Powershell配置文件的位置

  5. 5

    如何让Winrm默认使用Powershell 7进行远程会话

  6. 6

    如何使用Powershell Core 7解析HTML表?

  7. 7

    如何在PowerShell 7中使用AzureRm模块?

  8. 8

    如何在Windows 7中设置PowerShell默认窗口大小?

  9. 9

    如何使用PowerShell在Windows 7上检查IE版本?

  10. 10

    使用PowerShell读取大文件并删除回车

  11. 11

    如何在Azure Functions 3.x PowerShell 7中调用Azure PowerShell模块命令?

  12. 12

    如何从命令提示符执行Powershell脚本AS Powershell 7

  13. 13

    Powershell无法读取哈希

  14. 14

    如何通过Powershell运行7zip来解压缩.rar文件夹?

  15. 15

    如何从文件读取Powershell表

  16. 16

    在Windows 7(cmd或PowerShell)上的子文件夹中展平文件

  17. 17

    在Windows Server 2012上使用PowerShell删除7天以上的文件

  18. 18

    PowerShell-上次保存的文件-希望缩小到最近7天

  19. 19

    无法使用Powershell生成的随机密码打开7z文件

  20. 20

    Powershell脚本可返回最近7天内未创建/修改的Zip文件的目录

  21. 21

    我的 Windows 7 机器上 powershell 配置文件的位置不一致

  22. 22

    Windows 7和Server 2008中的Powershell-有区别吗?

  23. 23

    Powershell SOAP服务-找不到..和参数计数为“ 7”的重载

  24. 24

    Powershell 7-集内容和外文件将回车符(CR)添加到我的文件中

  25. 25

    使用PowerShell下载大文件

  26. 26

    如何使用Powershell在远程数据库上运行Entity Framework 7迁移?

  27. 27

    如何使PowerShell 4 cmdlet(如Test-NetConnection)在Windows 7上运行?

  28. 28

    安装3.5后如何从win7 powershell启动python 2.7?

  29. 29

    如何在PowerShell 7中对TTS使用Windows System.Speech(或有替代方法)

热门标签

归档