Base64.psm1

[CmdletBinding()]
param()
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath)
$script:PSModuleInfo = Test-ModuleManifest -Path "$PSScriptRoot\$baseName.psd1"
$script:PSModuleInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ }
$scriptName = $script:PSModuleInfo.Name
Write-Debug "[$scriptName] - Importing module"
#region [functions] - [public]
Write-Debug "[$scriptName] - [functions] - [public] - Processing folder"
#region [functions] - [public] - [ConvertFrom-Base64]
Write-Debug "[$scriptName] - [functions] - [public] - [ConvertFrom-Base64] - Importing"
filter ConvertFrom-Base64 {
    <#
        .SYNOPSIS
        Decodes a base64-encoded string into a UTF-8 string.

        .DESCRIPTION
        Converts a base64-encoded string into a human-readable UTF-8 string. 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.

        .OUTPUTS
        System.String

        .NOTES
        The decoded UTF-8 string.

        .LINK
        https://psmodule.io/Base64/Functions/ConvertFrom-Base64/
    #>

    [Alias('ConvertFrom-Base64String')]
    [OutputType([string])]
    [CmdletBinding()]
    param(
        # The base64-encoded string 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'
    )

    [System.Text.Encoding]::$Encoding.GetString([Convert]::FromBase64String($Base64String))
}
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"
filter ConvertTo-Base64 {
    <#
        .SYNOPSIS
        Converts a string to its 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.

        .EXAMPLE
        "Hello World" | ConvertTo-Base64

        Output:
        ```powershell
        SGVsbG8gV29ybGQ=
        ```

        Converts the string "Hello World" to its base64 encoded equivalent.

        .OUTPUTS
        System.String

        .NOTES
        The base64 encoded representation of the input string.

        .LINK
        https://psmodule.io/Base64/Functions/ConvertTo-Base64/
    #>

    [Alias('ConvertTo-Base64String')]
    [OutputType([string])]
    [CmdletBinding()]
    param(
        # The input string 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'
    )
    [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($String))
}
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