Public/Get-pseMDFileHashBase64.ps1

function Get-pseMDFileHashBase64 {
    <#
    .SYNOPSIS
    Calculates the MD5 hash of a file and returns it as a Base64-encoded string.
 
    .DESCRIPTION
    The Get-pseMDFileHashBase64 function calculates the MD5 hash of a file and returns it as a Base64-encoded string. It supports two parameter sets: FilePath and FileURL.
 
    .PARAMETER FilePath
    Specifies the path to the file for which the MD5 hash needs to be calculated. This parameter is mandatory when using the "FilePath" parameter set.
 
    .PARAMETER FileURL
    Specifies the URL of the file for which the MD5 hash needs to be calculated. This parameter is mandatory when using the "FileURL" parameter set.
 
    .INPUTS
    None. You cannot pipe input to this function.
 
    .OUTPUTS
    System.String
    The function returns a Base64-encoded string representing the MD5 hash of the file.
 
    .EXAMPLE
    PS> Get-pseMDFileHashBase64 -FilePath "C:\Files\example.txt"
    Calculates the MD5 hash of the file located at "C:\Files\example.txt" and returns the hash as a Base64-encoded string.
 
    .EXAMPLE
    PS> Get-pseMDFileHashBase64 -FileURL "https://example.com/files/example.txt"
    Calculates the MD5 hash of the file located at the specified URL and returns the hash as a Base64-encoded string.
 
    .NOTES
    Author: owen.heaume
    Version: 1.0.0 - Initial Release
#>


    [cmdletbinding(DefaultParameterSetName = "FilePath")]
    param (
        [parameter(Mandatory = $true, ParameterSetName = "FilePath")]
        $FilePath,

        [parameter(Mandatory = $true, ParameterSetName = "FileURL")]
        $FileURL
    )

    if ($PSCmdlet.ParameterSetName -eq "FilePath") {
        try {
            $rawMD5 = (Get-FileHash -Path $FilePath -Algorithm MD5).Hash

            # Create an array of bytes to store the hash values
            $hashBytes = [byte[]]::new($rawMD5.Length / 2)

            # Convert each pair of characters from the rawMD5 string to a byte and store it in the hashBytes array
            For ($i = 0; $i -lt $rawMD5.Length; $i += 2) {
                $hashBytes[$i / 2] = [convert]::ToByte($rawMD5.Substring($i, 2), 16)
            }

            # Convert the byte array to a Base64 string and return it
            return [system.convert]::ToBase64String($hashBytes)
        } catch {
            Write-Error $_.Exception.Message
        }
    }

    if ($PSCmdlet.ParameterSetName -eq "FileURL") {
        try {
            # Send a HEAD request to the specified URL to retrieve the header information
            $hashCheck = Invoke-WebRequest -Method Head -Uri $FileUrl -UseBasicParsing

            # Check if the request was successful (status code 200)
            if ($hashCheck.StatusCode -ne 200) {
                # If the request was not successful, throw a new WebException
                Write-Error [System.Net.WebException]::new('Failed to get header content.')
            }

            # Retrieve the Content-MD5 header value and return it
            return $($hashCheck.Headers['Content-MD5'])
        } catch {
            Write-Error $_
        }
    }
}