Skip to content

Commit

Permalink
Merge pull request #12 from PowerShell/base64-bytearray
Browse files Browse the repository at this point in the history
add support for byte arrays
  • Loading branch information
SteveL-MSFT committed Nov 24, 2020
2 parents 292a43f + 9ab4bf1 commit f1ce1db
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
64 changes: 57 additions & 7 deletions src/code/ConvertBase64Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,92 @@
// 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")]
[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)]
[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);
WriteObject(Encoding.UTF8.GetString(base64Bytes));

if (AsByteArray)
{
WriteObject(base64Bytes);
}
else
{
WriteObject(Encoding.UTF8.GetString(base64Bytes));
}
}
}

[Cmdlet(VerbsData.ConvertTo, "Base64")]
[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)]
[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()
{
var textBytes = Encoding.UTF8.GetBytes(Text);
WriteObject(Convert.ToBase64String(textBytes));
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));
}
}
}
}
22 changes: 22 additions & 0 deletions test/Base64.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Describe 'Base64 cmdlet tests' {
BeforeAll {
$testString = 'Hello World!'
$testBase64 = 'SGVsbG8gV29ybGQh'
$longString = $testString * 10
$longBase64 = "SGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29y`r`nbGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8gV29ybGQhSGVsbG8g`r`nV29ybGQh"
}

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

It 'ConvertTo-Base64 will accept text and insert breaklines' {
$longString | ConvertTo-Base64 -InsertBreakLines | Should -BeExactly $longBase64
}

It 'ConvertTo-Base64 will accept byte array' {
[System.Text.Encoding]::Utf8.GetBytes($testString) | ConvertTo-Base64 | Should -BeExactly $testBase64
}

It 'ConvertTo-Base64 will accept byte array and insert break lines' {
[System.Text.Encoding]::Utf8.GetBytes($longString) | ConvertTo-Base64 -InsertBreakLines | Should -BeExactly $longBase64
}

It 'ConvertFrom-Base64 will accept encoded input from parameter' {
ConvertFrom-Base64 -EncodedText $testBase64 | Should -BeExactly $testString
}
Expand All @@ -30,4 +44,12 @@ Describe 'Base64 cmdlet tests' {
It 'ConvertFrom-Base64 will accept encoded input form pipeline' {
$testBase64 | ConvertFrom-Base64 | Should -BeExactly $testString
}

It 'ConvertFrom-Base64 -AsByteArray returns byte array' {
($testBase64 | ConvertFrom-Base64 -AsByteArray) | Should -BeExactly ([System.Text.Encoding]::Utf8.GetBytes($testString))
}

It 'ConvertFrom-Base64 will accept text with breaks' {
$longBase64 | ConvertFrom-Base64 | Should -Be $longString
}
}

0 comments on commit f1ce1db

Please sign in to comment.