Skip to content

Instantly share code, notes, and snippets.

@majorpbx
Created March 11, 2021 06:36
Show Gist options
  • Save majorpbx/e91d1eb5160576861bc946e821c66ffe to your computer and use it in GitHub Desktop.
Save majorpbx/e91d1eb5160576861bc946e821c66ffe to your computer and use it in GitHub Desktop.
# Setup CRC32 function
Set-Alias crc32 Get-Crc32
<#
.NOTES
Author: greg zakharov
#>
$asm = Add-Type -Mem @'
[DllImport("ntdll.dll")]
internal static extern UInt32 RtlComputeCrc32(
UInt32 InitialCrc,
Byte[] Buffer,
Int32 Length
);
public static String ComputeCrc32(String file) {
UInt32 crc32 = 0;
Int32 read;
Byte[] buf = new Byte[4096];
using (FileStream fs = File.OpenRead(file)) {
while ((read = fs.Read(buf, 0, buf.Length)) != 0)
crc32 = RtlComputeCrc32(crc32, buf, read);
}
return ((crc32.ToString("X", CultureInfo.CurrentCulture)).PadLeft(8,'0'));
}
'@ -Name Check -NameSpace Crc32 -Using System.IO, System.Globalization -PassThru
# Start Hash value function
Function Get-HashValues {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ParameterSetName='File')]
[ValidateScript({If ((-Not (Test-Path -Path "$_")) -and ((Get-Item -Path "$_").Length -ne 0)) { } return $true })]
[String]$FilePath
)
$Script:HashValues = $NULL
$Script:HashValues = [ordered]@{"Name"="$($FilePath | Split-Path -Leaf)";"Size"="$((Get-Item $FilePath).Length)";"CRC32"="";"MD5"="";"SHA1"=""} # Removed: ;"SHA256"=""
If ([int32]$HashValues.Size -gt "99000000") {
Foreach ($i in $HashValues.Keys.Clone()) {
Switch ($i) {
"CRC32" {
If ($InputObject1 -eq $NULL) {} Else { $InputObject1.Dispose() } # Destroys the object if present
Try {
# $CRC = $asm::ComputeCrc32($($FilePath))
$HashValues[$i] = [string]$asm::ComputeCrc32($($FilePath))
} Catch {
$HashValues[$i] = "Error"
}
}
"MD5" {
$MD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$InputObject1 = [System.IO.File]::Open("$($FilePath)",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
$HashValues[$i] = ([System.BitConverter]::ToString($MD5.ComputeHash($InputObject1))).Replace("-","")
$InputObject1.Dispose()
}
"SHA1" {
$SHA1 = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
$InputObject1 = [System.IO.File]::Open("$($FilePath)",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
$HashValues[$i] = ([System.BitConverter]::ToString($SHA1.ComputeHash($InputObject1))).Replace("-","")
$InputObject1.Dispose()
}
"SHA256" {
$SHA256 = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
$InputObject1 = [System.IO.File]::Open("$($FilePath)",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
$HashValues[$i] = ([System.BitConverter]::ToString($SHA256.ComputeHash($InputObject1))).Replace("-","")
$InputObject1.Dispose()
}
}
}
} Else {
$InputObject2 = [System.IO.File]::ReadAllBytes("$($FilePath)")
Foreach ($i in $HashValues.Keys.Clone()) {
Switch ($i) {
"CRC32" {
Try {
# If ($InputObject2 -eq $NULL) {} Else { $InputObject2.Dispose() } # Destroys the object if present
# $CRC = $asm::ComputeCrc32($($FilePath))
$HashValues[$i] = [string]$asm::ComputeCrc32($($FilePath))
} Catch {
$HashValues[$i] = "Error"
}
}
"MD5" {
$Hasher = [System.Security.Cryptography.HashAlgorithm]::Create("$($i)")
[Byte[]]$ComputedHash = $Hasher.ComputeHash($InputObject2)
$HashValues[$i] = [string][System.BitConverter]::ToString($ComputedHash).Replace("-","")
}
"SHA1" {
$Hasher = [System.Security.Cryptography.HashAlgorithm]::Create("$($i)")
[Byte[]]$ComputedHash = $Hasher.ComputeHash($InputObject2)
$HashValues[$i] = [string][System.BitConverter]::ToString($ComputedHash).Replace("-","")
}
"SHA256" {
$Hasher = [System.Security.Cryptography.HashAlgorithm]::Create("$($i)")
[Byte[]]$ComputedHash = $Hasher.ComputeHash($InputObject2)
$HashValues[$i] = [string][System.BitConverter]::ToString($ComputedHash).Replace("-","")
}
}
}
}
}
# End Hash value function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment