/
Convert-StringToHash.ps1
48 lines (34 loc) · 1.13 KB
/
Convert-StringToHash.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
function Convert-StringToHash {
[CmdletBinding()]
[Alias("stringhash")]
Param (
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
Position = 0
)]
[ValidateNotNullOrEmpty()]
[string]
$String,
[Parameter(Position = 1)]
[ValidateSet('MACTripleDES', 'MD5', 'RIPEMD160', 'SHA1', 'SHA256', 'SHA512')]
[string]
$HashingAlgorithm = 'MD5'
)
begin {
$algorithm = [System.Security.Cryptography.HashAlgorithm]::Create($HashingAlgorithm) # Instantiate the algorithm
$encoder = [System.Text.Encoding]::UTF8 # Instantiate the string encoder
}
process {
$String | ForEach-Object {
$stringToBytes = $encoder.GetBytes($_)
[String]$hash = ''
$algorithm.ComputeHash($stringToBytes) | ForEach-Object {
# ComputeHash() method returns raw bytes
# Output each byte in hexadecimal and append to string
$hash += '{0:X}' -f $_
}
return $hash
}
}
}