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

Commit

Permalink
Add test for hashing of at_hash with all supported algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdecock committed Feb 21, 2024
1 parent 49595f9 commit a5e1176
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/OidcClient.Tests/CryptoHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Text;
using FluentAssertions;
using IdentityModel;
using IdentityModel.OidcClient;
using Xunit;

public class CryptoHelperTests
{
[Theory]
[InlineData("asdf", "RS256")]
[InlineData("asdf", "RS384")]
[InlineData("asdf", "RS512")]
public void ComputeHash_should_compute_correct_hashes_for_all_signature_algorithms(string data, string algorithmName)
{
var sut = new CryptoHelper(new OidcClientOptions());
var algorithm = sut.GetMatchingHashAlgorithm(algorithmName);

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 leftHalf = new byte[bytesInLeftHalf];
Array.Copy(hash, leftHalf, bytesInLeftHalf);

var hashString = Base64Url.Encode(leftHalf);

sut.ValidateHash(data, hashString, algorithmName).Should().BeTrue();
}

}

0 comments on commit a5e1176

Please sign in to comment.