new ps script, process directory with filter
This commit is contained in:
parent
6a15d54f08
commit
5c6c09b24f
98
sri.ps1
98
sri.ps1
@ -1,48 +1,96 @@
|
|||||||
param (
|
param (
|
||||||
# Specifies a path to one or more locations.
|
# List of files to hash
|
||||||
[Parameter(Mandatory=$true,
|
[Parameter(HelpMessage="Comma-separated list of files to hash.")]
|
||||||
HelpMessage="Path to resource for which to generate integrity hash.")]
|
[Alias("file", "list")]
|
||||||
[Alias("path", "resource")]
|
[ValidateNotNullOrEmpty()]
|
||||||
|
[string[]]
|
||||||
|
$files,
|
||||||
|
|
||||||
|
# Directory of files to hash
|
||||||
|
[Parameter(HelpMessage="Hash all files within this directory.")]
|
||||||
[ValidateNotNullOrEmpty()]
|
[ValidateNotNullOrEmpty()]
|
||||||
[string]
|
[string]
|
||||||
$filename,
|
$directory,
|
||||||
|
|
||||||
[Parameter(HelpMessage="Desired hash algorithm.")]
|
# File filter
|
||||||
|
[Parameter(HelpMessage="Only hash files of this type, relevant only when processing a directory.")]
|
||||||
|
[Alias("only")]
|
||||||
|
[ValidateNotNullOrEmpty()]
|
||||||
|
[string]
|
||||||
|
$filter = '*',
|
||||||
|
|
||||||
|
# Hash algorithm to use
|
||||||
|
[Parameter(HelpMessage="Hash algorithm to use (SHA256, SHA384, SHA512).")]
|
||||||
[Alias("algorithm")]
|
[Alias("algorithm")]
|
||||||
[ValidateSet('sha256', 'sha384', 'sha512')]
|
[ValidateSet('sha256', 'sha384', 'sha512')]
|
||||||
[string]
|
[string]
|
||||||
$hashAlgo='sha384'
|
$hashAlgo = 'sha384'
|
||||||
)
|
)
|
||||||
|
|
||||||
function hashSHA($type) {
|
function displayError($returnCode, $eMsg){
|
||||||
switch($type) {
|
Write-Host "`nERROR: $eMsg" -ForegroundColor Red
|
||||||
|
Write-Host "Exiting.`n" -ForegroundColor Red
|
||||||
|
exit $returnCode
|
||||||
|
}
|
||||||
|
|
||||||
|
function hashSHA($type){
|
||||||
|
switch($type){
|
||||||
'sha256' { return [System.Security.Cryptography.SHA256]::Create() }
|
'sha256' { return [System.Security.Cryptography.SHA256]::Create() }
|
||||||
'sha384' { return [System.Security.Cryptography.SHA384]::Create() }
|
'sha384' { return [System.Security.Cryptography.SHA384]::Create() }
|
||||||
'sha512' { return [System.Security.Cryptography.SHA512]::Create() }
|
'sha512' { return [System.Security.Cryptography.SHA512]::Create() }
|
||||||
default {
|
default{
|
||||||
Write-Host "`rUnknown hash algorithm.`r"
|
displayError 2 'Unknown hash algorithm.'
|
||||||
exit 2
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$fileContents = Get-Content $filename -Raw
|
function doHash($file, $hash){
|
||||||
|
$fileContents = Get-Content $file -Raw
|
||||||
|
try {
|
||||||
|
$hashBytes = $hash.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($fileContents))
|
||||||
|
return [System.Convert]::ToBase64String($hashBytes)
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# instantiate hash provider
|
||||||
$hashAlgo = $hashAlgo.ToLower()
|
$hashAlgo = $hashAlgo.ToLower()
|
||||||
$hashValue = hashSHA $hashAlgo
|
$hash = hashSHA $hashAlgo
|
||||||
|
|
||||||
try {
|
# process directory, if specified
|
||||||
$hashBytes = $hashValue.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($fileContents))
|
if ($directory){
|
||||||
$hashBase64 = [System.Convert]::ToBase64String($hashBytes)
|
# continue only if directory exists, otherwise exit with error
|
||||||
Write-Host "`r$hashAlgo-$hashBase64`r"
|
if (Test-Path -Path $directory){
|
||||||
}
|
Write-Host "`nProcessing directory: $directory" -ForegroundColor Cyan
|
||||||
catch {
|
Get-ChildItem -Path $directory -Filter $filter | ForEach-Object({
|
||||||
Write-Host "There was a problem generating a hash value."
|
$hashValue = doHash $directory\$_ $hash
|
||||||
exit 1
|
if ($hashValue -ne 1){
|
||||||
}
|
Write-Host "$_ --> $hashAlgo-$hashValue" -ForegroundColor Green
|
||||||
finally {
|
}
|
||||||
$hashValue.Dispose()
|
else {
|
||||||
|
Write-Host "$_ --> unable to hash file" -ForegroundColor Red
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
displayError 1 "Directory '$directory' does not exist."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# process file list, if specified
|
||||||
|
if ($files) {
|
||||||
|
Write-Host
|
||||||
|
foreach ($file in $files) {
|
||||||
|
Write-Host "Processing $file"
|
||||||
|
}
|
||||||
|
Write-Host
|
||||||
|
}
|
||||||
|
|
||||||
|
# clean up and exit
|
||||||
|
$hash.Dispose()
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
#EOF
|
#EOF
|
Loading…
Reference in New Issue
Block a user