This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathConvertBase64Command.cs
More file actions
93 lines (81 loc) · 2.88 KB
/
Copy pathConvertBase64Command.cs
File metadata and controls
93 lines (81 loc) · 2.88 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Text;
namespace Microsoft.PowerShell.TextUtility
{
[Cmdlet(VerbsData.ConvertFrom, "Base64", DefaultParameterSetName="Text")]
[OutputType(typeof(string))]
public sealed class ConvertFromBase64Command : PSCmdlet
{
/// <summary>
/// Gets or sets the base64 encoded string.
/// </summary>
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")]
public string EncodedText { get; set; }
/// <summary>
/// Gets or sets the AsByteArray switch.
/// </summary>
[Parameter()]
public SwitchParameter AsByteArray { get; set; }
protected override void ProcessRecord()
{
var base64Bytes = Convert.FromBase64String(EncodedText);
if (AsByteArray)
{
WriteObject(base64Bytes);
}
else
{
WriteObject(Encoding.UTF8.GetString(base64Bytes));
}
}
}
[Cmdlet(VerbsData.ConvertTo, "Base64", DefaultParameterSetName="Text")]
[OutputType(typeof(string))]
public sealed class ConvertToBase64Command : PSCmdlet
{
/// <summary>
/// Gets or sets the text to encoded to base64.
/// </summary>
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")]
public string Text { get; set; }
/// <summary>
/// Gets or sets the base64 encoded byte array.
/// </summary>
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="ByteArray")]
public byte[] ByteArray { get; set; }
/// <summary>
/// Gets or sets the InsertBreakLines switch.
/// </summary>
[Parameter()]
public SwitchParameter InsertBreakLines { get; set; }
private List<byte> _bytearray = new List<byte>();
private Base64FormattingOptions _base64Option = Base64FormattingOptions.None;
protected override void ProcessRecord()
{
if (InsertBreakLines)
{
_base64Option = Base64FormattingOptions.InsertLineBreaks;
}
if (ParameterSetName.Equals("Text"))
{
var textBytes = Encoding.UTF8.GetBytes(Text);
WriteObject(Convert.ToBase64String(textBytes, _base64Option));
}
else
{
_bytearray.AddRange(ByteArray);
}
}
protected override void EndProcessing()
{
if (ParameterSetName.Equals("ByteArray"))
{
WriteObject(Convert.ToBase64String(_bytearray.ToArray(), _base64Option));
}
}
}
}