Public/OAuth2/Get-ApiSignatureHeaders.ps1

<#
    .SYNOPSIS
    Create signature headers for API calls

    .DESCRIPTION

    .OUTPUTS
    Returns a hashtable to be used asan API signature header

    .PARAMETER DateString
    Date and time in yyyy-MM-ddTHH:mm:ss.fffZ format when the hsdp-api-signature is generated by the client. Defaults to now if not specified

    .EXAMPLE
    $headers = Get-ApiSignatureHeaders

    .LINK
    https://www.hsdp.io/documentation/identity-and-access-management-iam/api-documents/resource-reference-api/user-api-v2#/User%20Identity/post_authorize_identity_User
#>

function Get-ApiSignatureHeaders {

    [CmdletBinding()]
    [OutputType([Hashtable])]
    param(
        [Parameter(Mandatory = $false, Position = 0)]
        [String]$DateString
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

        $config = Get-Config
        $sharedKey = $config.AppCredentials.GetNetworkCredential().username
        $secretKey = $config.AppCredentials.GetNetworkCredential().password

        $secret = "DHPWS$($secretKey)"
        if (-not $DateString) {
            $DateString = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
        }

        $message = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($DateString))
        $hmacsha = New-Object System.Security.Cryptography.HMACSHA256
        $hmacsha.key = [Text.Encoding]::UTF8.GetBytes($secret)
        $signature = [Convert]::ToBase64String($hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($message)))

        Write-Output @{
            "hsdp-api-signature" = "HmacSHA256;Credential:$($sharedKey);SignedHeaders:SignedDate;Signature:$($signature)";
            "signeddate" = $DateString
        }
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }
}