Skip to main content

How to Check a JWT's HS256 Signature in the Terminal

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

When an app rejects a JSON Web Token with "invalid signature", the first question is whether the token was signed with the key you think it was. For tokens signed with HS256, you can answer that in the terminal: sign the token's first two parts again with the key and compare the result with its third part.

Syntax​

An HS256 signature is the HMAC-SHA256 of the token's header and payload, exactly as they appear in the token, with the dot between them, stored as base64url. The function recomputes it and compares.

In bash or zsh:

Syntax
jwt_check_hs256() {
local data expected
data=$(echo "$1" | cut -d. -f1-2)
expected=$(printf '%s' "$data" | openssl dgst -sha256 -hmac "$2" -binary | openssl base64 -A)
expected=$(printf '%s' "$expected" | tr '+/' '-_' | tr -d '=')
if [ "$expected" = "$(echo "$1" | cut -d. -f3)" ]; then
echo "valid signature"
else
echo "INVALID signature"
fi
}
jwt_check_hs256 '<token>' '<secret>'
PartWhat it does
cut -d. -f1-2Keeps the first two parts of the token, with the dot between them: the text that was signed.
openssl dgst -sha256 -hmac "$2" -binaryComputes HMAC-SHA256 of that text with the secret, the second argument, as raw bytes.
openssl base64 -A and tr '+/' '-_' | tr -d '='Turns the bytes into base64url, the way JWTs store signatures.
cut -d. -f3The token's own signature, its third part.
if [ … = … ]Compares the two and prints the result.

In PowerShell 7.6:

Syntax
function Test-JwtHs256([string] $Token, [string] $Secret) {
$parts = $Token.Split('.')
$key = [Text.Encoding]::UTF8.GetBytes($Secret)
$data = [Text.Encoding]::UTF8.GetBytes($parts[0] + '.' + $parts[1])
$hash = [Security.Cryptography.HMACSHA256]::HashData($key, $data)
$expected = [Convert]::ToBase64String($hash).TrimEnd('=').Replace('+', '-').Replace('/', '_')
if ($expected -ceq $parts[2]) { 'valid signature' } else { 'INVALID signature' }
}
Test-JwtHs256 '<token>' '<secret>'
PartWhat it does
$parts[0] + '.' + $parts[1]The signed text: header, dot, payload.
HMACSHA256::HashData($key, $data)HMAC-SHA256 with the secret's bytes as the key.
ToBase64String(…).TrimEnd('=').Replace(…)Turns the result into base64url.
-ceqCompares the two with letter case. PowerShell's plain -eq ignores case, and a and A are different characters in a signature.

Both functions use the secret's text as the key, which is how most JWT libraries treat a secret given as a string.

The Example​

A library app gives Omar a token that lets him read the catalogue for 30 minutes. The app signs its tokens with HS256 and this secret:

The token
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJvbWFyIiwic2NvcGUiOiJyZWFkOmJvb2tzIiwiaWF0IjoxNzk1NTk5NjgyLCJleHAiOjE3OTU2MDE0ODJ9.NOtuwU9W7ryNdIuGBYnyuKrRbN7q5sEMbnzdHDEfO3w
The app's secret (an example only)
XxTHoQ1p2jQrmY+2ZxDzgCeLWfJuiVwOdZGM3lLG1uE=

The check should pass with this secret and fail with any other.

Prerequisites​

  • macOS or Linux: OpenSSL. macOS includes it, and most Linux distributions do too; check with openssl version.
  • Windows: PowerShell 7.6, the current long-term support release.

Step 1: Store the Token and the Secret​

Terminal
token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJvbWFyIiwic2NvcGUiOiJyZWFkOmJvb2tzIiwiaWF0IjoxNzk1NTk5NjgyLCJleHAiOjE3OTU2MDE0ODJ9.NOtuwU9W7ryNdIuGBYnyuKrRbN7q5sEMbnzdHDEfO3w'
secret='XxTHoQ1p2jQrmY+2ZxDzgCeLWfJuiVwOdZGM3lLG1uE='

With a real secret, read it from an environment variable such as $JWT_SECRET instead of typing it, so it doesn't end up in your shell's history.

Step 2: Add the Function​

Paste the function into the same terminal. It prints nothing until you call it.

Terminal
jwt_check_hs256() {
local data expected
data=$(echo "$1" | cut -d. -f1-2)
expected=$(printf '%s' "$data" | openssl dgst -sha256 -hmac "$2" -binary | openssl base64 -A)
expected=$(printf '%s' "$expected" | tr '+/' '-_' | tr -d '=')
if [ "$expected" = "$(echo "$1" | cut -d. -f3)" ]; then
echo "valid signature"
else
echo "INVALID signature"
fi
}

Step 3: Check with the Right Secret, Then a Wrong One​

Terminal
jwt_check_hs256 "$token" "$secret"
jwt_check_hs256 "$token" "not-the-right-secret"
Output
valid signature
INVALID signature

A zsh terminal where the token and secret are stored, the jwt_check_hs256 function is defined, and the check prints valid signature with the app's secret and INVALID signature with not-the-right-secret.

The app's secret reproduces the signature exactly. Any other secret gives a completely different one, so the check fails. That's what makes a signature trustworthy: without the secret, nobody can produce one that matches.

What This Check Doesn't Do​

A valid signature proves the token was made with this secret and hasn't changed since. It doesn't prove the token should be accepted:

  • Expiry. The signature stays valid after exp has passed. An app must also check the time.
  • The algorithm. This works only for tokens whose header says "alg":"HS256". Decode the header first; How to Decode a JWT in the Terminal shows how.
  • Who it's for. A token signed for another app with the same secret would pass too.

The JWT course covers full validation later. Use this check to answer one question: was this token signed with this key?

Compare signatures safely in real code

An ordinary string comparison stops at the first different character, so it takes slightly longer the more characters match. Over many tries, that timing can help an attacker guess a signature. Applications should use a JWT library, or a constant-time comparison such as .NET's CryptographicOperations.FixedTimeEquals. In a terminal, for debugging, the comparison above is fine.

Common Mistakes​

MistakeWhat happensDo this instead
Signing the payload aloneThe signature never matchesSign header.payload, with the dot
Using -eq in PowerShellLetter case is ignored, so different signatures can look equalUse -ceq
Using the decoded key when the app uses the text, or the other way roundThe check fails with the right secretUse the key the way the app does
Treating "valid signature" as "valid token"An expired token still passesAlso check exp and the other claims

Troubleshooting​

SymptomLikely causeFix
INVALID signature with the secret you expectedThe token was signed with another key, or the app uses the key as decoded bytesCheck which key and form the issuing app uses
INVALID signature for a token from an identity providerIts header says RS256 or ES256, signed with a private keyThose need the provider's public key, not an HMAC secret
The function prints nothingIt was defined but not called, or the paste was cutPaste the whole function again, then call it

FAQ​

Why does a different secret give a completely different signature?​

HMAC is built so that changing any bit of the key or the message changes the result unpredictably. You can't adjust a signature a little; you either have the key or you don't.

Can I check RS256 tokens the same way?​

Not with an HMAC. RS256 uses a private key to sign and a public key to check, and the JWT course covers it in its own tutorial.

Is it safe to type the secret into the terminal?​

For a test key, yes. For a real one, avoid it: your shell may save the command in its history file. Read it from an environment variable instead.

Conclusion​

To check an HS256 signature, compute HMAC-SHA256 of the token's header.payload text with the secret, turn it into base64url, and compare it with the token's third part. jwt_check_hs256 and Test-JwtHs256 do that and print valid signature or INVALID signature. A match proves the token was signed with that key and hasn't changed; it doesn't prove the token is still valid.

Additional Resources​