Skip to content

Instantly share code, notes, and snippets.

@majorpbx
Created June 14, 2021 20:11
Show Gist options
  • Save majorpbx/bb20b369f6dfc33858b92432b5c1cfca to your computer and use it in GitHub Desktop.
Save majorpbx/bb20b369f6dfc33858b92432b5c1cfca to your computer and use it in GitHub Desktop.
Get-HashValues-New.ps1
# Start Hash value function
Function Get-HashValues {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[ValidateScript({If ((-Not (Test-Path -Path "$_")) -and ((Get-Item -Path "$_").Length -ne 0)) { } return $true })]
[String]$FilePath,
[Parameter(Mandatory=$False,ValueFromPipeline=$True)]
[Int]$SByte = 0,
[Parameter(Mandatory=$False,ValueFromPipeline=$True)]
[Int]$Bytes
)
Begin {
$Script:HashValues = $NULL
$Script:HashValues = [ordered]@{"Name"="";"Size"="";"CRC32"="";"MD5"="";"SHA1"=""} # Removed: ;"SHA256"=""
If (!($Bytes)) {
[String]$HashValues.Name = "$($FilePath | Split-Path -Leaf)"
[Int]$HashValues.Size = $((Get-Item $FilePath).Length)
} Else {
[String]$HashValues.Name = "bootsec"
[Int]$HashValues.Size = $($Bytes)
}
# Setup CRC32 function
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
public static class CRC32
{
[DllImport("ntdll.dll")]
public static extern UInt32 RtlComputeCrc32(
UInt32 InitialCrc,
Byte[] Buffer,
Int32 Length);
}
"@
# Setup Remaining Hash Collections
$MD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$SHA1 = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
# $SHA256 = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
$InputObject1 = [System.IO.File]::Open("$($FilePath)",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
$Buffer = [System.Byte[]]::new($($HashValues.Size))
$InputObject1.Position = 0
[void]$InputObject1.Read($Buffer,$SByte,$HashValues.Size)
}
Process {
Foreach ($i in $($HashValues.Keys.Clone())) {
Switch ($i) {
"CRC32" {
Try {
$HashValues[$i] = ('{0:X}' -f [CRC32]::RtlComputeCrc32(0, $Buffer, $HashValues.Size)).PadLeft(8,'0')
} Catch { $HashValues[$i] = "Error" }
}
"MD5" {
$HashValues[$i] = ([System.BitConverter]::ToString($MD5.ComputeHash($Buffer))).Replace("-","")
}
"SHA1" {
$HashValues[$i] = ([System.BitConverter]::ToString($SHA1.ComputeHash($Buffer))).Replace("-","")
}
# "SHA256" {
# $HashValues[$i] = ([System.BitConverter]::ToString($SHA256.ComputeHash($Buffer))).Replace("-","")
# }
}
}
}
End {
$InputObject1.Close()
$InputObject1.Dispose()
[System.GC]::Collect()
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
}
# End Hash value function
@majorpbx
Copy link
Author

majorpbx commented Sep 15, 2021

This script passes "$HashValues" to another script with all file and hash data for use in your script.

***** Warning: This has a high risk of crashing Powershell until I add size validation for "$SByte" and "$Bytes" *****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment