Skip to content

Namespace System.IO.Hashing usage with Powershell #115880

Open
@jaffa-bw

Description

@jaffa-bw

i create the following powershell script to calculate the MD5 and SHA512. i use chunks to use the loop for multiple algorithms and for speed.

$path = "C:\MyLocalFolder\somefile.iso"
$stream = [IO.File]::Open($Path, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)
$buffer = [Byte[]]::new(10MB)
$readCount = [int] 0

$a1 = [Security.Cryptography.HashAlgorithmName]::MD5
$a2 = [Security.Cryptography.HashAlgorithmName]::SHA512
$algorithm1 = [Security.Cryptography.HashAlgorithm]::Create($a1)
$algorithm2 = [Security.Cryptography.HashAlgorithm]::Create($a2)

while (($readCount = $stream.Read($buffer, 0, $buffer.Length)) -ne 0)
{
    $null = $algorithm1.TransformBlock($buffer, 0, $readCount, $null, 0)
    $null = $algorithm2.TransformBlock($buffer, 0, $readCount, $null, 0)
}

$null = $algorithm1.TransformFinalBlock($buffer, 0,0)
$null = $algorithm2.TransformFinalBlock($buffer, 0,0)
[BitConverter]::ToString($algorithm1.Hash).Replace('-','')
[BitConverter]::ToString($algorithm2.Hash).Replace('-','')

but, i need CRC32 additionally, which is available at System.IO.Hashing. this namespace is not available at powershell like Security.Cryptography.HashAlgorithm.
https://learn.microsoft.com/en-us/dotnet/api/system.io.hashing?view=net-9.0-pp

the dll is available at nuget, but import module is not working (error with "Could not load file or assembly 'System.Memory, Version=4.0.1.2, Culture=neutral").
https://www.nuget.org/packages/System.IO.Hashing/

briefly summarized: please make namespace System.IO.Hashing usable with powershell.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IO.HashingquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions