Home > Mobile >  How to write function to either Encode or Decode Base64 from either a string or path of file?
How to write function to either Encode or Decode Base64 from either a string or path of file?

Time:05-19

I have one function that will convert a string to base64 that works with simple strings. However something like:

$String = "$program = "notepad"; start $program"

Does not work, the extra quotes in the middle break the function. I tried using a script block but it does not work exactly right either. This is my first hurdle.
This is that function:

function B64-FromString{
    [CmdletBinding()]
    param (
    
    [Parameter (Mandatory = $True, ValueFromPipeline = $True)]
    [string]$Text
    )
    
    $Text = {$Text}
    $Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text)
    $EncodedText =[Convert]::ToBase64String($Bytes)
    $EncodedText
}

My next function takes a file path as a parameter and converts the contents of the file to base64 then returns the converted code. This function works as intended. This is that function:

function B64-FromFile {
    [CmdletBinding()]
    param (
    
    [Parameter (Mandatory = $True, ValueFromPipeline = $True)]
    [Alias("source")]
    [string]$so,

    [Parameter (Mandatory = $False)]
    [Alias("output")]
    [string]$Path, 

    [Parameter (Mandatory = $False)]
    [Alias("FileName")]
    [string]$File 

    )   

    if (!$File) { $File = "converted.txt" }

    if (!$Path) { $Path = [Environment]::GetFolderPath("Desktop") }
    
    $FilePath = ($Path "\" $File)

    $converted = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $so -Raw -Encoding UTF8)))
    
    return $converted
}

The end goal here is to have a single function that will have a parameter switch for encode/decode. And another switch for fromString/fromFile

i think using switches some where along the lines of:

Param(
    [Parameter(Position=0)]
    [string[]]
    $m
)
$mode = switch ( $m )
{
    "enc"  { "encode-function" }
    "dec" { "decode-function" }
}

}

and another parameter and switch to distinguish between recieving the input from a string or file contents.

I'm still learning powershell and having a hard time conceptualizing this particular methodology.

CodePudding user response:

So after about 10 hours of experimenting I came up with a solution that I am pretty proud of.

Still willing to take any constructive criticism.

function b64 {
[CmdletBinding(DefaultParameterSetName="encFile")]
param(
    [Parameter(Position=0, ParameterSetName="encFile")]
    [string]
    $encFile,

    [Parameter(Position=0, ParameterSetName="encString")]
    [string]
    $encString,

    [Parameter(Position=0, ParameterSetName="decFile")]
    [string]
    $decFile,

    [Parameter(Position=0, ParameterSetName="decString")]
    [string]
    $decString

)

if ($psCmdlet.ParameterSetName -eq "encFile") {
        $encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $encFile -Raw -Encoding UTF8)))
        return $encoded
        }

elseif ($psCmdlet.ParameterSetName -eq "encString") {
        $File = "$env:TEMP\foob64.txt"
        Set-Content -NoNewline -Path $File -Value $encString    
        $encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $File -Raw -Encoding UTF8)))
        Remove-Item $File
        return  $encoded
        }

elseif ($psCmdlet.ParameterSetName -eq "decFile") {
        $data = Get-Content $decFile
        $decoded = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($data))
        return $decoded     
        }

elseif ($psCmdlet.ParameterSetName -eq "decString") {
        $File = "$env:TEMP\foob64.txt"
        Set-Content -NoNewline -Path $File -Value $decString
        $data = Get-Content $File       
        $decoded = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($data))
        Remove-Item $File
        return $decoded
        }
}
  • Related