From e5081c95bc40ff8030e44b8eb4e87e9cfb4cdf55 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 May 2025 01:49:22 +0000 Subject: [PATCH 1/3] Initial plan for issue From 3779b32600d2b784467d4c257a642e9f75825861 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 May 2025 01:53:23 +0000 Subject: [PATCH 2/3] Add AsOneString parameter to ConvertTo-Base64 Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/public/ConvertTo-Base64.ps1 | 51 +++++++++++++++++++++-- tests/Base64.Tests.ps1 | 11 +++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/functions/public/ConvertTo-Base64.ps1 b/src/functions/public/ConvertTo-Base64.ps1 index 06185f6..d407724 100644 --- a/src/functions/public/ConvertTo-Base64.ps1 +++ b/src/functions/public/ConvertTo-Base64.ps1 @@ -6,6 +6,8 @@ .DESCRIPTION This function takes a string or array of strings as input and converts them to base64 encoded strings using UTF-8 encoding. It accepts input from the pipeline and can process string values directly or as an array. + + By default, each string is encoded individually. Use the -AsOneString parameter to join all inputs with newlines and encode as a single string. .EXAMPLE "Hello World" | ConvertTo-Base64 @@ -28,6 +30,26 @@ Converts each string in the array to its base64 encoded equivalent. + .EXAMPLE + @("Hello", "World") | ConvertTo-Base64 -AsOneString + + Output: + ```powershell + SGVsbG8KV29ybGQ= + ``` + + Joins all strings with newlines and encodes them as a single base64 string. + + .EXAMPLE + Get-Content -Path "file.txt" | ConvertTo-Base64 -AsOneString + + Output: + ```powershell + VGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgZmlsZQp3aXRoIG11bHRpcGxlIGxpbmVzLg== + ``` + + Reads the file content and encodes all lines as a single base64 string, similar to the Linux base64 utility. + .OUTPUTS System.String @@ -52,12 +74,35 @@ # The encoding to use when converting the string to bytes. [Parameter()] [ValidateSet('UTF8', 'UTF7', 'UTF32', 'ASCII', 'Unicode', 'BigEndianUnicode', 'Latin1')] - [string] $Encoding = 'UTF8' + [string] $Encoding = 'UTF8', + + # Join all input strings with newlines and encode as a single string. + [Parameter()] + [switch] $AsOneString ) + begin { + if ($AsOneString) { + [System.Collections.Generic.List[string]]$allStrings = @() + } + } + process { - foreach ($item in $String) { - [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($item)) + if ($AsOneString) { + foreach ($item in $String) { + $allStrings.Add($item) + } + } else { + foreach ($item in $String) { + [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($item)) + } + } + } + + end { + if ($AsOneString -and $allStrings.Count -gt 0) { + $joinedString = $allStrings -join [Environment]::NewLine + [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($joinedString)) } } } diff --git a/tests/Base64.Tests.ps1 b/tests/Base64.Tests.ps1 index 9d0002d..c818a89 100644 --- a/tests/Base64.Tests.ps1 +++ b/tests/Base64.Tests.ps1 @@ -70,6 +70,17 @@ Describe 'Base64' { $result[1] | Should -Be 'TGluZSAy' $result[2] | Should -Be 'TGluZSAz' } + + It "File content piped to ConvertTo-Base64 with AsOneString" { + $testFilePath = '/tmp/base64_test/test_content.txt' + $result = Get-Content -Path $testFilePath | ConvertTo-Base64 -AsOneString + $result | Should -Be 'TGluZSAxCkxpbmUgMgpMaW5lIDM=' + } + + It "@('Hello', 'World') | ConvertTo-Base64 -AsOneString -> single string" { + $result = @('Hello', 'World') | ConvertTo-Base64 -AsOneString + $result | Should -Be 'SGVsbG8KV29ybGQ=' + } } Context 'Function: ConvertFrom-Base64' { It "ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> ThisIsANiceString" { From d2eb57ee85453664528803257f777d670b2a8ee5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 May 2025 01:56:33 +0000 Subject: [PATCH 3/3] Update progress on Base64 joining feature Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- test.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 test.txt diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..d0bcae5 --- /dev/null +++ b/test.txt @@ -0,0 +1,3 @@ +hello some world! + +here is my decoded content