2026-02-22 19:11:17 +01:00
|
|
|
using System.Buffers.Text;
|
2026-02-08 11:57:57 +01:00
|
|
|
using System.Security.Cryptography;
|
2026-02-15 19:18:02 +01:00
|
|
|
using IdentityShroud.Core.Contracts;
|
2026-02-08 11:57:57 +01:00
|
|
|
using IdentityShroud.Core.Services;
|
|
|
|
|
|
|
|
|
|
namespace IdentityShroud.Core.Tests.Services;
|
|
|
|
|
|
|
|
|
|
public class EncryptionServiceTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RoundtripWorks()
|
|
|
|
|
{
|
2026-02-22 19:11:17 +01:00
|
|
|
// Note this code will tend to only test the latest verion.
|
|
|
|
|
|
2026-02-08 11:57:57 +01:00
|
|
|
// setup
|
2026-02-15 19:18:02 +01:00
|
|
|
var secretProvider = Substitute.For<ISecretProvider>();
|
2026-02-22 19:11:17 +01:00
|
|
|
secretProvider.GetSecret("Master").Returns("IGd9yUMusjNW0ezv8ink3QWlAHKFH45d21LyrbJTokw=");
|
2026-02-15 19:18:02 +01:00
|
|
|
|
2026-02-22 19:11:17 +01:00
|
|
|
ReadOnlySpan<byte> input = "Hello, World!"u8;
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
// act
|
2026-02-22 19:11:17 +01:00
|
|
|
EncryptionService sut = new(secretProvider);
|
|
|
|
|
byte[] cipher = sut.Encrypt(input.ToArray());
|
|
|
|
|
byte[] result = sut.Decrypt(cipher);
|
2026-02-08 11:57:57 +01:00
|
|
|
|
2026-02-22 19:11:17 +01:00
|
|
|
// verify
|
2026-02-08 11:57:57 +01:00
|
|
|
Assert.Equal(input, result);
|
|
|
|
|
}
|
2026-02-22 19:11:17 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void DecodeV1_Success()
|
|
|
|
|
{
|
|
|
|
|
// When introducing a new version we need version specific tests to
|
|
|
|
|
// make sure decoding of legacy data still works.
|
|
|
|
|
|
|
|
|
|
// setup
|
|
|
|
|
Span<byte> cipher =
|
|
|
|
|
[
|
|
|
|
|
1, 198, 55, 58, 56, 110, 238, 59, 158, 214, 85, 241, 26, 44, 140, 229, 128, 111, 167, 154, 160, 177, 152,
|
|
|
|
|
193, 74, 4, 235, 82, 207, 87, 32, 10, 239, 4, 246, 25, 21, 249, 25, 59, 160, 101
|
|
|
|
|
];
|
|
|
|
|
var secretProvider = Substitute.For<ISecretProvider>();
|
|
|
|
|
secretProvider.GetSecret("Master").Returns("IGd9yUMusjNW0ezv8ink3QWlAHKFH45d21LyrbJTokw=");
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
EncryptionService sut = new(secretProvider);
|
|
|
|
|
byte[] result = sut.Decrypt(cipher.ToArray());
|
|
|
|
|
|
|
|
|
|
// verify
|
|
|
|
|
Assert.Equal("Hello, World!"u8, result);
|
|
|
|
|
}
|
2026-02-08 11:57:57 +01:00
|
|
|
}
|