-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompare-FortiOSHash.ps1
More file actions
96 lines (79 loc) · 3 KB
/
Copy pathCompare-FortiOSHash.ps1
File metadata and controls
96 lines (79 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<#
.SYNOPSIS
Compare the hash of a downloaded file with a hash provided by Fortinet.
.DESCRIPTION
This script compares the hash of a downloaded file with a hash provided by Fortinet. It supports both MD5 and SHA512 hashes, and can check a TXT or XML file for the hash or take input from the command line. It also checks the name of the downloaded file to make sure it matches the provided name. The hashes are displayed side by side in all caps for easy comparison.
.PARAMETER FilePath
The path to the downloaded file.
.PARAMETER Hash
The hash provided by Fortinet. Can be provided via command line input or checked from a TXT or XML file.
.PARAMETER Algorithm
The hash algorithm to use. Supports MD5, SHA512, or both.
.PARAMETER Name
The expected name of the downloaded file.
.EXAMPLE
Compare-FortiOSHash -FilePath C:\Downloads\file.out -Hash ABCDEF123456 -Algorithm SHA512 -Name file.out
.INPUTS
None.
.OUTPUTS
None.
.NOTES
Version: 1.1
Author: Gabriel Jensen
#>
function Compare-FortiOSHash {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$FilePath,
[Parameter(Mandatory = $false)]
[string]$Hash,
[Parameter(Mandatory = $false)]
[ValidateSet('MD5', 'SHA512', 'Both')]
[string]$Algorithm = 'Both',
[Parameter(Mandatory = $false)]
[string]$Name
)
try {
# If hash is not provided, check TXT and XML files
if (-not $Hash) {
$hashFile = Join-Path $PSScriptRoot 'hash.txt'
if (Test-Path $hashFile) {
$Hash = Get-Content $hashFile
}
else {
$hashFile = Join-Path $PSScriptRoot 'hash.xml'
if (Test-Path $hashFile) {
$Hash = Select-Xml -Path $hashFile -XPath "//hash" | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty InnerText
}
else {
throw 'No hash provided and no hash.txt or hash.xml file found.'
}
}
}
# Check name of downloaded file
if ($Name -and (Split-Path $FilePath -Leaf) -ne $Name) {
throw "Downloaded file name doesn't match expected name."
}
# Check MD5 hash
if ($Algorithm -in 'MD5', 'Both') {
$md5 = Get-FileHash -Path $FilePath -Algorithm MD5 | Select-Object -ExpandProperty Hash
Write-Host "MD5: $($md5.ToUpper())"
if ($md5 -ne $Hash) {
throw 'MD5 hash does not match provided hash.'
}
}
# Check SHA512 hash
if ($Algorithm -in 'SHA512', 'Both') {
$sha512 = Get-FileHash -Path $FilePath -Algorithm SHA512 | Select-Object -ExpandProperty Hash
Write-Host "SHA512: $($sha512.ToUpper())"
if ($sha512 -ne $Hash) {
throw 'SHA512 hash does not match provided hash.'
}
}
}
catch {
Write-Error $_.Exception.Message
return
}
}