diff --git a/src/functions/public/ConvertFrom-Base64.ps1 b/src/functions/public/ConvertFrom-Base64.ps1 index 1162800..65fd60b 100644 --- a/src/functions/public/ConvertFrom-Base64.ps1 +++ b/src/functions/public/ConvertFrom-Base64.ps1 @@ -1,10 +1,10 @@ -filter ConvertFrom-Base64 { +function ConvertFrom-Base64 { <# .SYNOPSIS - Decodes a base64-encoded string into a UTF-8 string. + Decodes a base64-encoded string or array of strings into UTF-8 strings. .DESCRIPTION - Converts a base64-encoded string into a human-readable UTF-8 string. The function accepts input from the pipeline + Converts a base64-encoded string or array of strings into human-readable UTF-8 strings. The function accepts input from the pipeline and validates the input using the `Test-Base64` function before decoding. .EXAMPLE @@ -17,11 +17,22 @@ Decodes the base64-encoded string "U29tZSBkYXRh" into its original UTF-8 representation. + .EXAMPLE + @("SGVsbG8=", "V29ybGQ=") | ConvertFrom-Base64 + + Output: + ```powershell + Hello + World + ``` + + Decodes each base64-encoded string in the array into its original UTF-8 representation. + .OUTPUTS System.String .NOTES - The decoded UTF-8 string. + The decoded UTF-8 string(s). .LINK https://psmodule.io/Base64/Functions/ConvertFrom-Base64/ @@ -30,14 +41,14 @@ [OutputType([string])] [CmdletBinding()] param( - # The base64-encoded string to be decoded. + # The base64-encoded string or array of strings to be decoded. [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] [ValidateScript({ Test-Base64 -Base64String $_ }, ErrorMessage = 'Invalid Base64 string')] - [string] $Base64String, + [string[]] $Base64String, # The encoding to use when converting the string to bytes. [Parameter()] @@ -45,5 +56,9 @@ [string] $Encoding = 'UTF8' ) - [System.Text.Encoding]::$Encoding.GetString([Convert]::FromBase64String($Base64String)) + process { + foreach ($item in $Base64String) { + [System.Text.Encoding]::$Encoding.GetString([Convert]::FromBase64String($item)) + } + } } diff --git a/src/functions/public/ConvertTo-Base64.ps1 b/src/functions/public/ConvertTo-Base64.ps1 index 40dbd13..06185f6 100644 --- a/src/functions/public/ConvertTo-Base64.ps1 +++ b/src/functions/public/ConvertTo-Base64.ps1 @@ -1,11 +1,11 @@ -filter ConvertTo-Base64 { +function ConvertTo-Base64 { <# .SYNOPSIS - Converts a string to its base64 encoded representation. + Converts a string or array of strings to their base64 encoded representation. .DESCRIPTION - This function takes a string as input and converts it to a base64 encoded string using UTF-8 encoding. - It accepts input from the pipeline and can process string values directly. + 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. .EXAMPLE "Hello World" | ConvertTo-Base64 @@ -17,11 +17,22 @@ Converts the string "Hello World" to its base64 encoded equivalent. + .EXAMPLE + @("Hello", "World") | ConvertTo-Base64 + + Output: + ```powershell + SGVsbG8= + V29ybGQ= + ``` + + Converts each string in the array to its base64 encoded equivalent. + .OUTPUTS System.String .NOTES - The base64 encoded representation of the input string. + The base64 encoded representation of the input string(s). .LINK https://psmodule.io/Base64/Functions/ConvertTo-Base64/ @@ -30,18 +41,23 @@ [OutputType([string])] [CmdletBinding()] param( - # The input string to be converted to base64 encoding. + # The input string or array of strings to be converted to base64 encoding. [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] - [string] $String, + [string[]] $String, # The encoding to use when converting the string to bytes. [Parameter()] [ValidateSet('UTF8', 'UTF7', 'UTF32', 'ASCII', 'Unicode', 'BigEndianUnicode', 'Latin1')] [string] $Encoding = 'UTF8' ) - [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($String)) + + process { + foreach ($item in $String) { + [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($item)) + } + } } diff --git a/tests/Base64.Tests.ps1 b/tests/Base64.Tests.ps1 index 457967d..9d0002d 100644 --- a/tests/Base64.Tests.ps1 +++ b/tests/Base64.Tests.ps1 @@ -1,4 +1,29 @@ -Describe 'Base64' { +BeforeAll { + # Import functions directly + . "$PSScriptRoot/../src/functions/public/Test-Base64.ps1" + . "$PSScriptRoot/../src/functions/public/ConvertTo-Base64.ps1" + . "$PSScriptRoot/../src/functions/public/ConvertFrom-Base64.ps1" + + # Create test files + $testContentPath = '/tmp/base64_test/test_content.txt' + if (-not (Test-Path $testContentPath)) { + New-Item -ItemType Directory -Path '/tmp/base64_test' -Force | Out-Null + @" +Line 1 +Line 2 +Line 3 +"@ | Out-File -FilePath $testContentPath -Encoding UTF8 + } + + $testBase64Path = '/tmp/base64_test/test_base64_content.txt' + @" +TGluZSAx +TGluZSAy +TGluZSAz +"@ | Out-File -FilePath $testBase64Path -Encoding UTF8 +} + +Describe 'Base64' { Context 'Function: Test-Base64' { It "Test-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> true" { Test-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' | Should -Be $true @@ -15,6 +40,36 @@ It "'Hello World' | ConvertTo-Base64 -> SGVsbG8gV29ybGQ=" { 'Hello World' | ConvertTo-Base64 | Should -Be 'SGVsbG8gV29ybGQ=' } + It "ConvertTo-Base64 -String @('Hello', 'World') -> @('SGVsbG8=', 'V29ybGQ=')" { + $result = ConvertTo-Base64 -String @('Hello', 'World') + $result.Count | Should -Be 2 + $result[0] | Should -Be 'SGVsbG8=' + $result[1] | Should -Be 'V29ybGQ=' + } + + It "@('Hello', 'World') | ConvertTo-Base64 -> @('SGVsbG8=', 'V29ybGQ=')" { + $result = @('Hello', 'World') | ConvertTo-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'SGVsbG8=' + $result[1] | Should -Be 'V29ybGQ=' + } + + It "Variable containing multiple strings piped to ConvertTo-Base64" { + $strings = @('Hello', 'World') + $result = $strings | ConvertTo-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'SGVsbG8=' + $result[1] | Should -Be 'V29ybGQ=' + } + + It "File content piped to ConvertTo-Base64" { + $testFilePath = '/tmp/base64_test/test_content.txt' + $result = Get-Content -Path $testFilePath | ConvertTo-Base64 + $result.Count | Should -Be 3 + $result[0] | Should -Be 'TGluZSAx' + $result[1] | Should -Be 'TGluZSAy' + $result[2] | Should -Be 'TGluZSAz' + } } Context 'Function: ConvertFrom-Base64' { It "ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> ThisIsANiceString" { @@ -24,5 +79,36 @@ It "'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 -> Hello World" { 'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 | Should -Be 'Hello World' } + It "ConvertFrom-Base64 -Base64String @('SGVsbG8=', 'V29ybGQ=') -> @('Hello', 'World')" { + $result = ConvertFrom-Base64 -Base64String @('SGVsbG8=', 'V29ybGQ=') + $result.Count | Should -Be 2 + $result[0] | Should -Be 'Hello' + $result[1] | Should -Be 'World' + } + + It "@('SGVsbG8=', 'V29ybGQ=') | ConvertFrom-Base64 -> @('Hello', 'World')" { + $result = @('SGVsbG8=', 'V29ybGQ=') | ConvertFrom-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'Hello' + $result[1] | Should -Be 'World' + } + + It "Variable containing multiple Base64 strings piped to ConvertFrom-Base64" { + $base64Strings = @('SGVsbG8=', 'V29ybGQ=') + $result = $base64Strings | ConvertFrom-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'Hello' + $result[1] | Should -Be 'World' + } + + It "File content with Base64 strings piped to ConvertFrom-Base64" { + $testBase64FilePath = '/tmp/base64_test/test_base64_content.txt' + @('TGluZSAx', 'TGluZSAy', 'TGluZSAz') | Out-File -FilePath $testBase64FilePath + $result = Get-Content -Path $testBase64FilePath | ConvertFrom-Base64 + $result.Count | Should -Be 3 + $result[0] | Should -Be 'Line 1' + $result[1] | Should -Be 'Line 2' + $result[2] | Should -Be 'Line 3' + } } }