-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashfile.ps1
86 lines (67 loc) · 2.27 KB
/
hashfile.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Powershell wrapper around the rust_hash.exe binary
# This will parse the output into useful objects
# call like this:
# $result = .\hashfile.ps1 "C:\Users\user\Downloads\*.exe" -algorithm sha3
[CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
PositionalBinding = $false,
ConfirmImpact = 'Medium')]
param (
# a search glob passed direct to the exe, eg '*.txt'
[Parameter(Mandatory = $false, ParameterSetName = 'Parameter Set 1', Position = 0)][string]$path,
# files passed as pipeline objects, eg dir | sha3.ps1
[parameter(Mandatory = $false, ParameterSetName = 'Parameter Set 1',
ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string[]]$inputfile,
# algorithm, eg MD5, SHA1, SHA256, SHA384, SHA512, SHA3-256, SHA3-384, SHA3-512
[string]$algorithm = "SHA3",
# limit the number of files to hash
[int]$limit = 0
)
begin {
# runs once at start of script
class FileHashJS {
[string]$Hash
[System.IO.FileInfo]$File
FileHashJS(
[string]$line
) {
$split = $line.Split(' ', 2)
$this.Hash = $split[0]
$this.File = [System.IO.FileInfo] $split[1]
}
}
$pipelinelist = New-Object Collections.Generic.List[string]
}
process {
# run this for every pipeline object
if ($inputfile) {
$pipelinelist.Add($inputfile)
}
}
end {
# runs once at end of script. This does all the work
$piped = $pipelinelist.Count -gt 0
if (-not $piped -and $path.Length -eq 0) {
throw "No path or piped input"
}
if ($limit -gt 0) {
# a limit is set
if ($piped) {
$raw = $pipelinelist | hash_rust.exe --limit $limit --algorithm $algorithm
}
else {
$raw = hash_rust.exe --limit $limit --algorithm $algorithm $path
}
}
else {
if ($piped) {
$raw = $pipelinelist | hash_rust.exe --algorithm $algorithm
}
else {
$raw = hash_rust.exe --algorithm $algorithm $path
}
}
# turn the raw output into powershell objects
$results = $raw | ForEach-Object { [FileHashJS]::new($_) }
$results
}