Skip to content

Commit a5e1176

Browse files
committed
Add test for hashing of at_hash with all supported algorithms
1 parent 49595f9 commit a5e1176

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
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.
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)