PoshCode Archive  Artifact [228ab4b878]

Artifact 228ab4b8782eb1aa7761b0625ece9e9a484f99fa837df0c684cdff98102354ee:

  • File Hash-Checker.ps1 — part of check-in [1604dec8c8] at 2018-06-10 13:53:47 on branch trunk — 93ef94849b2d23cc2b73a9a4aaecbdfc60e6f6fd71c38f7dffbd553c45b31c894b6e37e3ec4221ad5496aa7a67925f79d2866748e5e3e00758dbfa386523306e (user: socialhype size: 2425)

# encoding: ascii
# api: powershell
# title: Hash Checker
# description: 93ef94849b2d23cc2b73a9a4aaecbdfc60e6f6fd71c38f7dffbd553c45b31c894b6e37e3ec4221ad5496aa7a67925f79d2866748e5e3e00758dbfa386523306e
# version: 0.1
# type: script
# author: socialhype
# license: CC0
# function: Test-Hash
# x-poshcode-id: 5346
# x-archived: 2015-01-31T07:58:08
# x-published: 2015-07-31T22:23:00
#
#
#.Synopsis
#   Test the HMAC hash(es) of a file
#.Description
#   Takes the HMAC hash of a file using specified algorithm, and optionally, compare it to a baseline hash
#.Example
#   Test-Hash npp.5.3.1.Installer.exe -HashFile npp.5.3.1.release.md5
# 
#   Searches the provided hash file for a line matching the "npp.5.3.1.Installer.exe" file name
#   and take the hash of the file (using the extension of the HashFile as the Type of Hash).
#
#.Example
#   Test-Hash npp.5.3.1.Installer.exe 360293affe09ffc229a3f75411ffa9a1 MD5
#
#   Takes the MD5 hash and compares it to the provided hash
#
#.Example
#   Test-Hash npp.5.3.1.Installer.exe 5e6c2045f4ddffd459e6282f3ff1bd32b7f67517 
#
#   Tests all of the hashes against the provided (Sha1) hash
#
function Test-Hash { 
#[CmdletBinding(DefaultParameterSetName="NoExpectation")]
PARAM(
   #[Parameter(Position=0,Mandatory=$true)]
   [string]$FileName
,
   #[Parameter(Position=2,Mandatory=$true,ParameterSetName="ManualHash")]
   [string]$ExpectedHash = $(if($HashFileName){  ((Get-Content $HashFileName) -match $FileName)[0].split(" ")[0]  })
,
   #[Parameter(Position=1,Mandatory=$true,ParameterSetName="FromHashFile")]
   [string]$HashFileName
,
   #[Parameter(Position=1,Mandatory=$true,ParameterSetName="ManualHash")]
   [string[]]$TypeOfHash = $(if($HashFileName){  
                          [IO.Path]::GetExtension((Convert-Path $HashFileName)).Substring(1) 
                 } else { "MD5","SHA1","SHA256","SHA384","SHA512","RIPEMD160" })
)
$ofs=""
  $hashes = @{}
  foreach($Type in $TypeOfHash) {
    [string]$hash = [Security.Cryptography.HashAlgorithm]::Create(
      $Type
    ).ComputeHash( 
      [IO.File]::ReadAllBytes( (Convert-Path $FileName) )
    ) | ForEach { "{0:x2}" -f $_ }
    $hashes.$Type = $hash
  }
  
  if($ExpectedHash) {
    ($hashes.Values -eq $hash).Count -ge 1
  } else {
    foreach($hash in $hashes.GetEnumerator()) {
      "{0,-8}{1}" -f $hash.Name, $hash.Value
    }        
  }
}