using System.Buffers.Text; using System.Security.Cryptography; using IdentityShroud.Core.Contracts; using IdentityShroud.Core.Services; namespace IdentityShroud.Core.Tests.Services; public class EncryptionServiceTests { [Fact] public void RoundtripWorks() { // Note this code will tend to only test the latest verion. // setup var secretProvider = Substitute.For(); secretProvider.GetSecret("Master").Returns("IGd9yUMusjNW0ezv8ink3QWlAHKFH45d21LyrbJTokw="); ReadOnlySpan input = "Hello, World!"u8; // act EncryptionService sut = new(secretProvider); byte[] cipher = sut.Encrypt(input.ToArray()); byte[] result = sut.Decrypt(cipher); // verify Assert.Equal(input, result); } [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 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(); secretProvider.GetSecret("Master").Returns("IGd9yUMusjNW0ezv8ink3QWlAHKFH45d21LyrbJTokw="); // act EncryptionService sut = new(secretProvider); byte[] result = sut.Decrypt(cipher.ToArray()); // verify Assert.Equal("Hello, World!"u8, result); } }