File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ function Get-FileHash {
2+ <#
3+ . SYNOPSIS
4+ Returns hash from a file
5+
6+ . DESCRIPTION
7+ Returns hash from a file using given hash algorithm.
8+ Default algorithm is: 'MD5'
9+
10+ . PARAMETER FileName
11+ Name/path of the file to be hashed
12+
13+ . PARAMETER HashName
14+ Hash algorithm name
15+
16+ . EXAMPLE
17+ Get-FileHash README.md
18+ Returns hash from "README.md" file, which is located in the current directory
19+
20+ . EXAMPLE
21+ Get-FileHash "C:\README.md"
22+ Returns hash from "README.md" file, which is location is "C:\README.md"
23+
24+ . EXAMPLE
25+ Get-FileHash README.md -HashName "sha256"
26+ Returns hash from "README.md" file, which is located in the current directory using 'sha256' algorithm
27+
28+ #>
29+
30+ [CmdletBinding ()]
31+ param (
32+ [Parameter (Mandatory = $true , Position = 0 )]
33+ [String ]$FileName ,
34+ [Parameter (Mandatory = $false , Position = 1 )]
35+ [String ]$HashName = " MD5"
36+ )
37+
38+ process {
39+ if (! (Test-Path $FileName )) {
40+ $curLoc = Get-Location
41+ $FileName = Join-Path $curLoc $FileName
42+ }
43+ if (! (Test-Path $FileName )) {
44+ Write-Error " Could not find file $FileName "
45+ exit
46+ }
47+ $FileName = Get-Item - Path $FileName
48+ $FileStream = New-Object System.IO.FileStream($FileName , [System.IO.FileMode ]::Open)
49+ $StringBuilder = New-Object System.Text.StringBuilder
50+ [System.Security.Cryptography.HashAlgorithm ]::Create($HashName ).ComputeHash($FileStream ) | % { [Void ]$StringBuilder.Append ($_.ToString (" x2" )) }
51+ $FileStream.Close ()
52+ $FileStream.Dispose ()
53+ $StringBuilder.ToString ()
54+ }
55+ }
You can’t perform that action at this time.
0 commit comments