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

Commit a3530c3

Browse files
authored
Merge pull request #410 from IdentityModel/joe/at_hash-algorithms
Fix at_hash calculation for RS384, RS512
2 parents 184e077 + 263978c commit a3530c3

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/OidcClient/CryptoHelper.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public bool ValidateHash(string data, string hashedData, string signatureAlgorit
5656
using (hashAlgorithm)
5757
{
5858
var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(data));
59-
var size = (hashAlgorithm.HashSize / 8) / 2;
59+
var size = hashAlgorithm.HashSize / 8 / 2; // Only take the left half of the data, as per spec for at_hash
6060

61-
byte[] leftPart = new byte[hashAlgorithm.HashSize / size];
62-
Array.Copy(hash, leftPart, hashAlgorithm.HashSize / size);
61+
byte[] leftPart = new byte[size];
62+
Array.Copy(hash, leftPart, size);
6363

6464
var leftPartB64 = Base64Url.Encode(leftPart);
6565
var match = leftPartB64.Equals(hashedData);
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Text;
3+
using FluentAssertions;
4+
using IdentityModel;
5+
using IdentityModel.OidcClient;
6+
using Xunit;
7+
8+
public class CryptoHelperTests
9+
{
10+
[Theory]
11+
[InlineData("asdf", "RS256")]
12+
[InlineData("asdf", "RS384")]
13+
[InlineData("asdf", "RS512")]
14+
public void ComputeHash_should_compute_correct_hashes_for_all_signature_algorithms(string data, string algorithmName)
15+
{
16+
var sut = new CryptoHelper(new OidcClientOptions());
17+
var algorithm = sut.GetMatchingHashAlgorithm(algorithmName);
18+
19+
var hash = algorithm.ComputeHash(Encoding.ASCII.GetBytes(data));
20+
21+
var bytesInLeftHalf = algorithm.HashSize / 16; // Divide by 8 for bytes and then 2 to get just half, as per spec for at_hash.
22+
23+
var leftHalf = new byte[bytesInLeftHalf];
24+
Array.Copy(hash, leftHalf, bytesInLeftHalf);
25+
26+
var hashString = Base64Url.Encode(leftHalf);
27+
28+
sut.ValidateHash(data, hashString, algorithmName).Should().BeTrue();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)