Skip to content
This repository has been archived by the owner on Feb 23, 2025. It is now read-only.

Commit

Permalink
Fix at_hash calculation for RS384, RS512
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdecock committed Feb 21, 2024
1 parent a5e1176 commit 263978c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/OidcClient/CryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public bool ValidateHash(string data, string hashedData, string signatureAlgorit
using (hashAlgorithm)
{
var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(data));
var size = (hashAlgorithm.HashSize / 8) / 2;
var size = hashAlgorithm.HashSize / 8 / 2; // Only take the left half of the data, as per spec for at_hash

byte[] leftPart = new byte[hashAlgorithm.HashSize / size];
Array.Copy(hash, leftPart, hashAlgorithm.HashSize / size);
byte[] leftPart = new byte[size];
Array.Copy(hash, leftPart, size);

var leftPartB64 = Base64Url.Encode(leftPart);
var match = leftPartB64.Equals(hashedData);
Expand Down
2 changes: 1 addition & 1 deletion test/OidcClient.Tests/CryptoHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void ComputeHash_should_compute_correct_hashes_for_all_signature_algorith

var hash = algorithm.ComputeHash(Encoding.ASCII.GetBytes(data));

var bytesInLeftHalf = algorithm.HashSize / 16; // Divide by 8 for bytes and then 2 to get just half.
var bytesInLeftHalf = algorithm.HashSize / 16; // Divide by 8 for bytes and then 2 to get just half, as per spec for at_hash.

var leftHalf = new byte[bytesInLeftHalf];
Array.Copy(hash, leftHalf, bytesInLeftHalf);
Expand Down

0 comments on commit 263978c

Please sign in to comment.