Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit f1ce1db

Browse files
authored
Merge pull request #12 from PowerShell/base64-bytearray
add support for byte arrays
2 parents 292a43f + 9ab4bf1 commit f1ce1db

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/code/ConvertBase64Command.cs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,92 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Management.Automation;
67
using System.Text;
78

89
namespace Microsoft.PowerShell.TextUtility
910
{
10-
[Cmdlet(VerbsData.ConvertFrom, "Base64")]
11+
[Cmdlet(VerbsData.ConvertFrom, "Base64", DefaultParameterSetName="Text")]
1112
[OutputType(typeof(string))]
1213
public sealed class ConvertFromBase64Command : PSCmdlet
1314
{
1415
/// <summary>
1516
/// Gets or sets the base64 encoded string.
1617
/// </summary>
17-
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true)]
18+
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")]
1819
public string EncodedText { get; set; }
1920

21+
/// <summary>
22+
/// Gets or sets the AsByteArray switch.
23+
/// </summary>
24+
[Parameter()]
25+
public SwitchParameter AsByteArray { get; set; }
26+
2027
protected override void ProcessRecord()
2128
{
2229
var base64Bytes = Convert.FromBase64String(EncodedText);
23-
WriteObject(Encoding.UTF8.GetString(base64Bytes));
30+
31+
if (AsByteArray)
32+
{
33+
WriteObject(base64Bytes);
34+
}
35+
else
36+
{
37+
WriteObject(Encoding.UTF8.GetString(base64Bytes));
38+
}
2439
}
2540
}
2641

27-
[Cmdlet(VerbsData.ConvertTo, "Base64")]
42+
[Cmdlet(VerbsData.ConvertTo, "Base64", DefaultParameterSetName="Text")]
2843
[OutputType(typeof(string))]
2944
public sealed class ConvertToBase64Command : PSCmdlet
3045
{
3146
/// <summary>
3247
/// Gets or sets the text to encoded to base64.
3348
/// </summary>
34-
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true)]
49+
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")]
3550
public string Text { get; set; }
3651

52+
/// <summary>
53+
/// Gets or sets the base64 encoded byte array.
54+
/// </summary>
55+
[Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="ByteArray")]
56+
public byte[] ByteArray { get; set; }
57+
58+
/// <summary>
59+
/// Gets or sets the InsertBreakLines switch.
60+
/// </summary>
61+
[Parameter()]
62+
public SwitchParameter InsertBreakLines { get; set; }
63+
64+
private List<byte> _bytearray = new List<byte>();
65+
private Base64FormattingOptions _base64Option = Base64FormattingOptions.None;
66+
3767
protected override void ProcessRecord()
3868
{
39-
var textBytes = Encoding.UTF8.GetBytes(Text);
40-
WriteObject(Convert.ToBase64String(textBytes));
69+
if (InsertBreakLines)
70+
{
71+
_base64Option = Base64FormattingOptions.InsertLineBreaks;
72+
}
73+
74+
if (ParameterSetName.Equals("Text"))
75+
{
76+
var textBytes = Encoding.UTF8.GetBytes(Text);
77+
WriteObject(Convert.ToBase64String(textBytes, _base64Option));
78+
}
79+
else
80+
{
81+
_bytearray.AddRange(ByteArray);
82+
}
83+
}
84+
85+
protected override void EndProcessing()
86+
{
87+
if (ParameterSetName.Equals("ByteArray"))
88+
{
89+
WriteObject(Convert.ToBase64String(_bytearray.ToArray(), _base64Option));
90+
}
4191
}
4292
}
4393
}

test/Base64.tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Describe 'Base64 cmdlet tests' {
55
BeforeAll {
66
$testString = 'Hello World!'
77
$testBase64 = 'SGVsbG8gV29ybGQh'
8+
$longString = $testString * 10
9+
$longBase64 = "SGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29y`r`nbGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8g`r`nV29ybGQh"
810
}
911

1012
It 'ConvertTo-Base64 will accept text input from parameter' {
@@ -19,6 +21,18 @@ Describe 'Base64 cmdlet tests' {
1921
$testString | ConvertTo-Base64 | Should -BeExactly $testBase64
2022
}
2123

24+
It 'ConvertTo-Base64 will accept text and insert breaklines' {
25+
$longString | ConvertTo-Base64 -InsertBreakLines | Should -BeExactly $longBase64
26+
}
27+
28+
It 'ConvertTo-Base64 will accept byte array' {
29+
[System.Text.Encoding]::Utf8.GetBytes($testString) | ConvertTo-Base64 | Should -BeExactly $testBase64
30+
}
31+
32+
It 'ConvertTo-Base64 will accept byte array and insert break lines' {
33+
[System.Text.Encoding]::Utf8.GetBytes($longString) | ConvertTo-Base64 -InsertBreakLines | Should -BeExactly $longBase64
34+
}
35+
2236
It 'ConvertFrom-Base64 will accept encoded input from parameter' {
2337
ConvertFrom-Base64 -EncodedText $testBase64 | Should -BeExactly $testString
2438
}
@@ -30,4 +44,12 @@ Describe 'Base64 cmdlet tests' {
3044
It 'ConvertFrom-Base64 will accept encoded input form pipeline' {
3145
$testBase64 | ConvertFrom-Base64 | Should -BeExactly $testString
3246
}
47+
48+
It 'ConvertFrom-Base64 -AsByteArray returns byte array' {
49+
($testBase64 | ConvertFrom-Base64 -AsByteArray) | Should -BeExactly ([System.Text.Encoding]::Utf8.GetBytes($testString))
50+
}
51+
52+
It 'ConvertFrom-Base64 will accept text with breaks' {
53+
$longBase64 | ConvertFrom-Base64 | Should -Be $longString
54+
}
3355
}

0 commit comments

Comments
 (0)