-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test-Module.psm1
66 lines (61 loc) · 1.96 KB
/
Test-Module.psm1
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
Function ConvertTo-Base64 {
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$True,Mandatory=$True,HelpMessage="String to encode to base64")]
[ValidateNotNullOrEmpty()]
$String
)
Process {
$StringToByteArr = [System.Text.Encoding]::UTF8.GetBytes($String) #Converts string to byte array
$Base64String = [System.Convert]::ToBase64String($StringToByteArr) #Converts byte array to b64 string
Return $Base64String
}
} #End ConvertTo-Base64
Function ConvertFrom-Base64 {
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$True,Mandatory=$True,HelpMessage="String to decode from base64")]
[ValidateNotNullOrEmpty()]
$String
)
Process {
$Base64ToUTF8 = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($String)) #Converts b64 string to UTF-8 string
Return $Base64ToUTF8
}
} #End ConvertFrom-Base64
Function Get-StringHash {
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$True,Mandatory=$True,HelpMessage="String to hash")]
$String,
[Parameter(HelpMessage="Hash algorithm")]
[ValidateSet('MD5','RIPEMD160','SHA1','SHA256','SHA384','SHA512')]
$Algorithm = "SHA1"
)
Process {
$StringBuilder = New-Object System.Text.StringBuilder
$ByteHash = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))
ForEach ($Byte in $ByteHash) {
[Void]$StringBuilder.Append($Byte.ToString("x2")) #Convert byte array to hex
}
$Hash = $StringBuilder.ToString()
Return $Hash
}
} #End Get-StringHash
Function Get-URL {
[CmdletBinding()]
param (
$URL,
[switch]$Encode
)
[void][Reflection.Assembly]::LoadWithPartialName("System.Web")
$ProcessedURL = [System.Web.HttpUtility]::UrlDecode($URL)
If ($PSBoundParameters.Encode) {
$ProcessedURL = [System.Web.HttpUtility]::UrlEncode($URL)
}
Return $ProcessedURL
} #End Get-URL
Export-ModuleMember ConvertTo-Base64
Export-ModuleMember ConvertFrom-Base64
Export-ModuleMember Get-StringHash
Export-ModuleMember Get-URL