diff --git a/src/code/ConvertBase64Command.cs b/src/code/ConvertBase64Command.cs
index ff0aa75..0c4b39d 100644
--- a/src/code/ConvertBase64Command.cs
+++ b/src/code/ConvertBase64Command.cs
@@ -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
{
///
/// Gets or sets the base64 encoded string.
///
- [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true)]
+ [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")]
public string EncodedText { get; set; }
+ ///
+ /// Gets or sets the AsByteArray switch.
+ ///
+ [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
{
///
/// Gets or sets the text to encoded to base64.
///
- [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true)]
+ [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="Text")]
public string Text { get; set; }
+ ///
+ /// Gets or sets the base64 encoded byte array.
+ ///
+ [Parameter(Position=0, Mandatory=true, ValueFromPipeline=true, ParameterSetName="ByteArray")]
+ public byte[] ByteArray { get; set; }
+
+ ///
+ /// Gets or sets the InsertBreakLines switch.
+ ///
+ [Parameter()]
+ public SwitchParameter InsertBreakLines { get; set; }
+
+ private List _bytearray = new List();
+ 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));
+ }
}
}
}
diff --git a/test/Base64.tests.ps1 b/test/Base64.tests.ps1
index 304dc97..cccda33 100644
--- a/test/Base64.tests.ps1
+++ b/test/Base64.tests.ps1
@@ -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' {
@@ -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
}
@@ -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
+ }
}