forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathConvertFrom-Base64.ps1
73 lines (59 loc) · 2.07 KB
/
ConvertFrom-Base64.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
<#
.SYNOPSIS
Converts base64-encoded text to bytes or text.
.INPUTS
System.String of base64-encoded text to decode.
.OUTPUTS
System.String or System.Byte[] of decoded text or data.
.FUNCTIONALITY
Data encoding
.LINK
https://docs.microsoft.com/dotnet/api/system.convert.frombase64string
.LINK
https://www.rfc-editor.org/rfc/rfc7515.html#appendix-C
.EXAMPLE
ConvertFrom-Base64.ps1 dXNlcm5hbWU6QmFkUEBzc3dvcmQ= -Encoding utf8
username:BadP@ssword
.EXAMPLE
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' |ConvertFrom-Base64.ps1 -Encoding ascii -UriStyle
{"alg":"HS256","typ":"JWT"}
.EXAMPLE
'77u/dHJ1ZQ0K' |ConvertFrom-Base64.ps1 |Format-Hex
Label: Byte (System.Byte) <3D01E7E8>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 EF BB BF 74 72 75 65 0D 0A true��
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string])][OutputType([byte[]])] Param(
# The base64-encoded data.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string] $Data,
<#
Uses the specified encoding to convert the bytes in the data to text.
If no encoding is provided, the data will be returned as a byte array.
#>
[Parameter(Position=1)][ValidateSet('ascii','byte','utf16','utf16BE','utf32','utf32BE','utf7','utf8')]
[string] $Encoding = 'byte',
<#
Indicates that the URI-friendly variant of the base64 algorithm should be used.
This variant, as used by JWTs, uses - instead of +, and _ instead of /, and trims the = padding at the end
to avoid extra escaping within URLs or URL-encoded data.
#>
[switch] $UriStyle
)
Process
{
if($UriStyle)
{
$Data = $Data -replace '-','+' -replace '_','/'
$Data += '='*(3-(($Data.Length-1)% 4))
}
$value = [Convert]::FromBase64String($Data)
if($Encoding -eq 'byte') {$value}
else
{
$encoder = [Text.Encoding]::GetEncoding(($Encoding -replace '^utf','utf-'))
$encoder.GetString($value)
}
}