MarkDowd (Community Member) asked a question.

Is there an example of using HMAC to access the API from PowerShell?

The following documentation page provides examples of doing this in C#, Java and Python. Has anyone done it from PowerShell? We keep getting different signatures than when we do it from Java.

https://docs.veracode.com/r/c_enabling_hmac

 

Java (works in Postman)

----

function computeHashHex(message, key_hex) {

    console.info('Full: ', CryptoJS.HmacSHA256(message, CryptoJS.enc.Hex.parse(key_hex)).toString(CryptoJS.enc.Hex));

    return CryptoJS.HmacSHA256(message, CryptoJS.enc.Hex.parse(key_hex)).toString(CryptoJS.enc.Hex);

 

PowerShell (produces a different hash using the same parameters)

------------

function computeHashHex(

    [string] $message,

    [string] $key_hex

)

{

    try {

        $hmacsha = New-Object System.Security.Cryptography.HMACSHA256

        $hmacsha.key = [Text.Encoding]::UTF8.GetBytes($key_hex)

        $hash = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($message))

        $signature = [System.BitConverter]::ToString($hash).Replace("-","").ToLower()

    } finally {

        $hmacsha.Dispose()

    }

    return $signature

}


Topics (2)

No articles found
Loading

Ask the Community

Get answers, share a use case, discuss your favorite features, or get input from the community.