public void RoundtripCertificate_Should_Succeed()
            {
                // Arrange
                var originalRsaParameters = RSACryptoServiceProvider.Create(RsaKey.KeySize).ExportParameters(false);

                // Act
                var model         = SerializationModelConverter.Convert(rsaParameters: originalRsaParameters);
                var rsaParameters = SerializationModelConverter.Convert(rsaSerializationModel: model);

                // Assert
                model.D.Should().BeNull();
                rsaParameters.D.Should().BeNull();
                model.DP.Should().BeNull();
                rsaParameters.DP.Should().BeNull();
                model.DQ.Should().BeNull();
                rsaParameters.DQ.Should().BeNull();
                System.Convert.FromBase64String(model.Exponent).SequenceEqual(rsaParameters.Exponent).Should().BeTrue();
                model.InverseQ.Should().BeNull();
                rsaParameters.InverseQ.Should().BeNull();
                System.Convert.FromBase64String(model.Modulus).SequenceEqual(rsaParameters.Modulus).Should().BeTrue();
                model.P.Should().BeNull();
                rsaParameters.P.Should().BeNull();
                model.Q.Should().BeNull();
                rsaParameters.Q.Should().BeNull();
            }
 public void RsaSerializationModelInvalid_ShouldThrow_NotSupportedException()
 {
     // Arrange
     // Act & Assert
     Assert.Throws <NotSupportedException>(
         () => SerializationModelConverter.Convert(
             rsaSerializationModel: new RsaSerializationModel
     {
         D = "invalidBase64",
     }));
 }
            public void WithEmbeddedDataButWithoutSignature_Should_Succeed()
            {
                // Arrange
                var originalKey = RsaKey.Generate(embeddedData: new byte[] { 0x01, 0x02, 0x03, 0x03 });

                // Act
                var model = SerializationModelConverter.Convert(key: originalKey);
                var key   = SerializationModelConverter.ConvertKeyModel(model: model);

                // Assert
                System.Convert.FromBase64String(model.Hash).SequenceEqual(originalKey.Hash.Hash).Should().BeTrue();
                key.Hash.Hash.SequenceEqual(originalKey.Hash.Hash).Should().BeTrue();
                System.Convert.FromBase64String(model.EmbeddedData).SequenceEqual(originalKey.EmbeddedData).Should().BeTrue();
                key.EmbeddedData.SequenceEqual(originalKey.EmbeddedData).Should().BeTrue();
                model.SignerCertificateHash.Should().BeNull();
                model.Signature.Should().BeNull();
                key.Signature.Should().BeNull();
            }
            public void WithoutEmbeddedDataAndWithoutSignature_Should_Succeed()
            {
                // Arrange
                var originalKey         = RsaKey.Generate();
                var originalCertificate = (RsaCertificate)originalKey.DeriveCertificate();

                // Act
                var model       = SerializationModelConverter.Convert(certificate: originalCertificate);
                var certificate = SerializationModelConverter.ConvertCertificateModel(model: model);

                // Assert
                System.Convert.FromBase64String(model.Hash).SequenceEqual(originalCertificate.Hash.Hash).Should().BeTrue();
                certificate.Hash.Hash.SequenceEqual(originalCertificate.Hash.Hash).Should().BeTrue();
                model.EmbeddedData.Should().BeNull();
                certificate.EmbeddedData.Should().BeNull();
                model.SignerCertificateHash.Should().BeNull();
                model.Signature.Should().BeNull();
                certificate.Signature.Should().BeNull();
            }
            public void WithoutEmbeddedDataButWithSignature_Should_Succeed()
            {
                // Arrange
                var signerKey   = RsaKey.Generate();
                var originalKey = RsaKey.Generate(signKeyCallback: hash => (RsaSignature)signerKey.Sign(hash));

                // Act
                var model = SerializationModelConverter.Convert(key: originalKey);
                var key   = SerializationModelConverter.ConvertKeyModel(model: model);

                // Assert
                System.Convert.FromBase64String(model.Hash).SequenceEqual(originalKey.Hash.Hash).Should().BeTrue();
                key.Hash.Hash.SequenceEqual(originalKey.Hash.Hash).Should().BeTrue();
                model.EmbeddedData.Should().BeNull();
                key.EmbeddedData.Should().BeNull();
                System.Convert.FromBase64String(model.SignerCertificateHash).SequenceEqual(originalKey.Signature.SignerCertificateHash.Hash).Should().BeTrue();
                System.Convert.FromBase64String(model.Signature).SequenceEqual(originalKey.Signature.Signature).Should().BeTrue();
                key.Signature.SignerCertificateHash.Hash.SequenceEqual(originalKey.Signature.SignerCertificateHash.Hash).Should().BeTrue();
                key.Signature.Signature.SequenceEqual(originalKey.Signature.Signature).Should().BeTrue();
            }
            public void WithEmbeddedDataAndWithSignature_Should_Succeed()
            {
                // Arrange
                var signerKey   = RsaKey.Generate();
                var originalKey = RsaKey.Generate(
                    embeddedData: new byte[] { 0x01, 0x02, 0x03, 0x03 },
                    signKeyCallback: hash => (RsaSignature)signerKey.Sign(hash));
                var originalCertificate = (RsaCertificate)originalKey.DeriveCertificate();

                // Act
                var model       = SerializationModelConverter.Convert(certificate: originalCertificate);
                var certificate = SerializationModelConverter.ConvertCertificateModel(model: model);

                // Assert
                System.Convert.FromBase64String(model.Hash).SequenceEqual(originalCertificate.Hash.Hash).Should().BeTrue();
                certificate.Hash.Hash.SequenceEqual(originalCertificate.Hash.Hash).Should().BeTrue();
                System.Convert.FromBase64String(model.EmbeddedData).SequenceEqual(originalCertificate.EmbeddedData).Should().BeTrue();
                certificate.EmbeddedData.SequenceEqual(originalCertificate.EmbeddedData).Should().BeTrue();
                System.Convert.FromBase64String(model.SignerCertificateHash).SequenceEqual(originalCertificate.Signature.SignerCertificateHash.Hash).Should().BeTrue();
                System.Convert.FromBase64String(model.Signature).SequenceEqual(originalCertificate.Signature.Signature).Should().BeTrue();
                certificate.Signature.SignerCertificateHash.Hash.SequenceEqual(originalCertificate.Signature.SignerCertificateHash.Hash).Should().BeTrue();
                certificate.Signature.Signature.SequenceEqual(originalCertificate.Signature.Signature).Should().BeTrue();
            }
 public void CertificateNull_ShouldThrow_ArgumentNullException()
 {
     // Arrange
     // Act & Assert
     Assert.Throws <ArgumentNullException>(() => SerializationModelConverter.Convert(certificate: null));
 }