Skip to content

Commit b18c8fa

Browse files
committed
Added Get-StringHash cmdlet
1 parent daabca0 commit b18c8fa

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

strings/Get-StringHash.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function Get-StringHash {
2+
<#
3+
.SYNOPSIS
4+
Returns hash from a string
5+
6+
.DESCRIPTION
7+
Returns hash from an input string using given hash algorithm.
8+
Default algorithm is: 'MD5'
9+
10+
.PARAMETER String
11+
String to be hashed
12+
13+
.PARAMETER HashName
14+
Hash algorithm name
15+
16+
.EXAMPLE
17+
Get-StringHash "test"
18+
Returns hash from string 'test'
19+
20+
.EXAMPLE
21+
Get-StringHash 'test' -HashName "sha256"
22+
Returns hash from string 'test' using 'sha256' algorithm
23+
24+
#>
25+
26+
[CmdletBinding()]
27+
param(
28+
[Parameter(Mandatory = $true, Position = 0)]
29+
[String]$String,
30+
[Parameter(Mandatory = $false, Position = 1)]
31+
[String]$HashName = "MD5"
32+
)
33+
34+
process {
35+
$StringBuilder = New-Object System.Text.StringBuilder
36+
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String)) | % {
37+
[Void]$StringBuilder.Append($_.ToString("x2"))
38+
}
39+
$StringBuilder.ToString()
40+
}
41+
}

0 commit comments

Comments
 (0)