public void AddHeader_BeyondCapacity() { const int Count = 20; var cache = new JwtHeaderCache(); var rawHeaders = new byte[Count][]; for (int i = 0; i < Count; i++) { rawHeaders[i] = new byte[32]; RandomNumberGenerator.Fill(rawHeaders[i]); JwtHeader header = JwtHeader.FromJson($"{{\"kid\":\"{i}\"}}"); cache.AddHeader(rawHeaders[i], header); Assert.Equal(header, cache.Head); } Assert.Equal("19", cache.Head.Kid); Assert.Equal("10", cache.Tail.Kid); for (int i = 0; i < 10; i++) { Assert.False(cache.TryGetHeader(rawHeaders[i], out var header)); } for (int i = 10; i < Count; i++) { Assert.True(cache.TryGetHeader(rawHeaders[i], out var header)); } }
public void AddHeader_Lru() { var cache = new JwtHeaderCache(); var rawHeaders = new byte[10][]; for (int i = 0; i < 10; i++) { rawHeaders[i] = new byte[32]; RandomNumberGenerator.Fill(rawHeaders[i]); JwtHeader header = JwtHeader.FromJson($"{{\"kid\":\"{i}\"}}"); cache.AddHeader(rawHeaders[i], header); } for (int i = 0; i < 10; i++) { Assert.True(cache.TryGetHeader(rawHeaders[i], out var header)); Assert.Equal(header, cache.Head); Assert.NotEqual(header, cache.Tail); } }
public void Unwrap2() { var kwp = new EcdhKeyWrapper(_bobKey, EncryptionAlgorithm.Aes128CbcHmacSha256, KeyManagementAlgorithm.EcdhEsAes128KW); byte[] wrappedKey = new byte[kwp.GetKeyWrapSize()]; var header = new JwtObject(); header.Add(new JwtProperty(HeaderParameters.ApuUtf8, Base64Url.Encode("Alice"))); header.Add(new JwtProperty(HeaderParameters.ApvUtf8, Base64Url.Encode("Bob"))); var cek = kwp.WrapKey(_aliceKey, header, wrappedKey); var kuwp = new EcdhKeyUnwrapper(_bobKey, EncryptionAlgorithm.Aes128CbcHmacSha256, KeyManagementAlgorithm.EcdhEsAes128KW); var apu = Encoding.UTF8.GetString(Base64Url.Encode("Alice"));; var apv = Encoding.UTF8.GetString(Base64Url.Encode("Bob")); var epk = ((JwtObject)header[HeaderParameters.EpkUtf8].Value).ToString(); var jwtHeader = JwtHeader.FromJson($"{{\"apu\":\"{apu}\",\"apv\":\"{apv}\",\"epk\":{epk}}}"); byte[] unwrappedKey = new byte[kuwp.GetKeyUnwrapSize(wrappedKey.Length)]; var unwrapped = kuwp.TryUnwrapKey(wrappedKey, unwrappedKey, jwtHeader, out int bytesWritten); Assert.True(unwrapped); }