Base64.psm1
[Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSAvoidAssignmentToAutomaticVariable', 'IsWindows', Justification = 'IsWindows doesnt exist in PS5.1' )] [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseDeclaredVarsMoreThanAssignments', 'IsWindows', Justification = 'IsWindows doesnt exist in PS5.1' )] [CmdletBinding()] param() $baseName = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath) $script:PSModuleInfo = Import-PowerShellDataFile -Path "$PSScriptRoot\$baseName.psd1" $script:PSModuleInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ } $scriptName = $script:PSModuleInfo.Name Write-Debug "[$scriptName] - Importing module" if ($PSEdition -eq 'Desktop') { $IsWindows = $true } #region [functions] - [public] Write-Debug "[$scriptName] - [functions] - [public] - Processing folder" #region [functions] - [public] - [ConvertFrom-Base64] Write-Debug "[$scriptName] - [functions] - [public] - [ConvertFrom-Base64] - Importing" function ConvertFrom-Base64 { <# .SYNOPSIS Decodes a base64-encoded string or array of strings into UTF-8 strings. .DESCRIPTION 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 "U29tZSBkYXRh" | ConvertFrom-Base64 Output: ```powershell Some data ``` 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(s). .LINK https://psmodule.io/Base64/Functions/ConvertFrom-Base64/ #> [Alias('ConvertFrom-Base64String')] [OutputType([string])] [CmdletBinding()] param( # 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, # The encoding to use when converting the string to bytes. [Parameter()] [ValidateSet('UTF8', 'UTF7', 'UTF32', 'ASCII', 'Unicode', 'BigEndianUnicode', 'Latin1')] [string] $Encoding = 'UTF8' ) process { foreach ($item in $Base64String) { [System.Text.Encoding]::$Encoding.GetString([Convert]::FromBase64String($item)) } } } Write-Debug "[$scriptName] - [functions] - [public] - [ConvertFrom-Base64] - Done" #endregion [functions] - [public] - [ConvertFrom-Base64] #region [functions] - [public] - [ConvertTo-Base64] Write-Debug "[$scriptName] - [functions] - [public] - [ConvertTo-Base64] - Importing" function ConvertTo-Base64 { <# .SYNOPSIS Converts a string or array of strings to their base64 encoded representation. .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. .EXAMPLE "Hello World" | ConvertTo-Base64 Output: ```powershell SGVsbG8gV29ybGQ= ``` 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(s). .LINK https://psmodule.io/Base64/Functions/ConvertTo-Base64/ #> [Alias('ConvertTo-Base64String')] [OutputType([string])] [CmdletBinding()] param( # The input string or array of strings to be converted to base64 encoding. [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] [string[]] $String, # The encoding to use when converting the string to bytes. [Parameter()] [ValidateSet('UTF8', 'UTF7', 'UTF32', 'ASCII', 'Unicode', 'BigEndianUnicode', 'Latin1')] [string] $Encoding = 'UTF8' ) process { foreach ($item in $String) { [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($item)) } } } Write-Debug "[$scriptName] - [functions] - [public] - [ConvertTo-Base64] - Done" #endregion [functions] - [public] - [ConvertTo-Base64] #region [functions] - [public] - [Test-Base64] Write-Debug "[$scriptName] - [functions] - [public] - [Test-Base64] - Importing" filter Test-Base64 { <# .SYNOPSIS Determines whether a given string is a valid base64-encoded string. .DESCRIPTION This function checks whether the provided string is a valid base64-encoded string. It attempts to decode the input using `[Convert]::FromBase64String()`. If the decoding succeeds, it returns `$true`; otherwise, it returns `$false`. .EXAMPLE Test-Base64 -Base64String 'U29tZSBkYXRh' Output: ```powershell True ``` Returns `$true` as the string is a valid base64-encoded string. .EXAMPLE 'U29tZSBkYXRh' | Test-Base64 Output: ```powershell True ``` Returns `$true` as the string is a valid base64-encoded string. .OUTPUTS bool .NOTES Returns `$true` if the string is a valid base64-encoded string, otherwise `$false`. .LINK https://psmodule.io/Test/Functions/Test-Base64 #> [OutputType([bool])] [CmdletBinding()] param ( # The base64-encoded string to validate. [Parameter( Mandatory, ValueFromPipeline )] [string] $Base64String ) try { $null = [Convert]::FromBase64String($Base64String) $true } catch { $false } } Write-Debug "[$scriptName] - [functions] - [public] - [Test-Base64] - Done" #endregion [functions] - [public] - [Test-Base64] Write-Debug "[$scriptName] - [functions] - [public] - Done" #endregion [functions] - [public] #region Member exporter $exports = @{ Alias = '*' Cmdlet = '' Function = @( 'ConvertFrom-Base64' 'ConvertTo-Base64' 'Test-Base64' ) Variable = '' } Export-ModuleMember @exports #endregion Member exporter |