Home > Hints, PowerShell > HMAC SHA1 using PowerShell

HMAC SHA1 using PowerShell

The following bit will create a hex-formatted HMAC SHA1 hash of a “string” using a “secret”,

$hmacsha = New-Object System.Security.Cryptography.HMACSHA1
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes("secret")
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes("string"))
[string]::join("", ($signature | % {([int]$_).toString('x2')}))

If you look closely at the snippet itself, you’ll notice it assumes your strings are ASCII-encoded, so you may need to adjust it slightly in some cases.

By the way, the answer is ee8ac90d2d72885a4247f86addea4c3c29a4cba4.

Advertisement
Categories: Hints, PowerShell
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.