Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/functions/public/ConvertTo-Base64.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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))
}
}
}
3 changes: 3 additions & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello some world!

here is my decoded content
11 changes: 11 additions & 0 deletions tests/Base64.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Loading