/// <summary>
    /// Validates that this <see cref="CngCbcAuthenticatedEncryptorConfiguration"/> is well-formed, i.e.,
    /// that the specified algorithms actually exist and that they can be instantiated properly.
    /// An exception will be thrown if validation fails.
    /// </summary>
    void IInternalAlgorithmConfiguration.Validate()
    {
        var factory = new CngCbcAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);

        // Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
        using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
        {
            encryptor.PerformSelfTest();
        }
    }
    private static IAuthenticatedEncryptor CreateEncryptorInstanceFromDescriptor(CngCbcAuthenticatedEncryptorDescriptor descriptor)
    {
        var encryptorFactory = new CngCbcAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
        var key = new Key(
            Guid.NewGuid(),
            DateTimeOffset.Now,
            DateTimeOffset.Now + TimeSpan.FromHours(1),
            DateTimeOffset.Now + TimeSpan.FromDays(30),
            descriptor,
            new[] { encryptorFactory });

        return(key.CreateEncryptor());
    }
    public void CreateEncrptorInstance_UnknownDescriptorType_ReturnsNull()
    {
        // Arrange
        var key = new Mock <IKey>();

        key.Setup(k => k.Descriptor).Returns(new Mock <IAuthenticatedEncryptorDescriptor>().Object);

        var factory = new CngCbcAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);

        // Act
        var encryptor = factory.CreateEncryptorInstance(key.Object);

        // Assert
        Assert.Null(encryptor);
    }
    public void CreateEncrptorInstance_ExpectedDescriptorType_ReturnsEncryptor()
    {
        // Arrange
        var descriptor = new CngCbcAuthenticatedEncryptorConfiguration().CreateNewDescriptor();
        var key        = new Mock <IKey>();

        key.Setup(k => k.Descriptor).Returns(descriptor);

        var factory = new CngCbcAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);

        // Act
        var encryptor = factory.CreateEncryptorInstance(key.Object);

        // Assert
        Assert.NotNull(encryptor);
        Assert.IsType <CbcAuthenticatedEncryptor>(encryptor);
    }