-
-
Notifications
You must be signed in to change notification settings - Fork 766
/
Calculate-Hash.ps1
68 lines (48 loc) · 1.67 KB
/
Calculate-Hash.ps1
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
function Calculate-Hash {
<#
.SYNOPSIS
PowerShell v2 compatible script to calculate file hashes.
.PARAMETER Path
Path to file.
.PARAMETER Algorithm
Algorithm to use: MD5, RIPEMD160, SHA1, SHA256, SHA384, SHA512. If the algorithm parameter is not specified both MD5 and SHA256 will be shown.
.DESCRIPTION
Author: Ruben Boonen (@FuzzySec)
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
.EXAMPLE
C:\PS> Calculate-Hash -Path C:\Some\File.path -Algorithm SHA512
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$false)]
[ValidateSet($null,"MD5","RIPEMD160","SHA1","SHA256","SHA384","SHA512")]
[string]$Algorithm=$null
)
# Make sure the path is valid
$PathCheck = Test-Path $Path
if($PathCheck -eq "True"){
function HashCalc($HashAlgorithm){
$FileStream = [system.io.file]::openread((resolve-path $Path))
$HashObject = [System.Security.Cryptography.HashAlgorithm]::create($HashAlgorithm)
$Hash = $HashObject.ComputeHash($FileStream)
$FileStream.close()
$FileStream.dispose()
$Hash = [system.bitconverter]::tostring($Hash).replace('-','')
echo "$HashAlgorithm : $Hash"
}
if($Algorithm){
HashCalc $Algorithm
}
else{
HashCalc "MD5"
HashCalc "SHA256"
}
}
else{
echo "File path is not valid, maybe more coffee is the answer?"
}
}