예제 #1
0
        public void UsesKeyVaultToEncryptKey()
        {
            var keyMock = new Mock <IKeyEncryptionKey>(MockBehavior.Strict);

            keyMock.Setup(client => client.WrapKeyAsync("RSA-OAEP", It.IsAny <ReadOnlyMemory <byte> >(), default))
            .ReturnsAsync((string _, ReadOnlyMemory <byte> data, CancellationToken __) => data.ToArray().Reverse().ToArray())
            .Verifiable();

            keyMock.SetupGet(client => client.KeyId).Returns("KeyId");

            var mock = new Mock <IKeyEncryptionKeyResolver>();

            mock.Setup(client => client.ResolveAsync("key", default))
            .ReturnsAsync((string _, CancellationToken __) => keyMock.Object)
            .Verifiable();

            var encryptor = new AzureKeyVaultXmlEncryptor(mock.Object, "key", new MockNumberGenerator());
            var result    = encryptor.Encrypt(new XElement("Element"));

            var encryptedElement = result.EncryptedElement;
            var value            = encryptedElement.Element("value");

            mock.VerifyAll();
            Assert.NotNull(result);
            Assert.NotNull(value);
            Assert.AreEqual(typeof(AzureKeyVaultXmlDecryptor), result.DecryptorType);
            Assert.AreEqual("VfLYL2prdymawfucH3Goso0zkPbQ4/GKqUsj2TRtLzsBPz7p7cL1SQaY6I29xSlsPQf6IjxHSz4sDJ427GvlLQ==", encryptedElement.Element("value").Value);
            Assert.AreEqual("AAECAwQFBgcICQoLDA0ODw==", encryptedElement.Element("iv").Value);
            Assert.AreEqual("Dw4NDAsKCQgHBgUEAwIBAA==", encryptedElement.Element("key").Value);
            Assert.AreEqual("KeyId", encryptedElement.Element("kid").Value);
        }
예제 #2
0
        public void UsesKeyVaultToEncryptKey()
        {
            var mock = new Mock <IKeyVaultWrappingClient>();

            mock.Setup(client => client.WrapKeyAsync("key", JsonWebKeyEncryptionAlgorithm.RSAOAEP, It.IsAny <byte[]>()))
            .Returns <string, string, byte[]>((_, __, data) => Task.FromResult(new KeyOperationResult("KeyId", data.Reverse().ToArray())));

            var encryptor = new AzureKeyVaultXmlEncryptor(mock.Object, "key", new MockNumberGenerator());
            var result    = encryptor.Encrypt(new XElement("Element"));

            var encryptedElement = result.EncryptedElement;
            var value            = encryptedElement.Element("value");

            mock.VerifyAll();
            Assert.NotNull(result);
            Assert.NotNull(value);
            Assert.Equal(typeof(AzureKeyVaultXmlDecryptor), result.DecryptorType);
            Assert.Equal("VfLYL2prdymawfucH3Goso0zkPbQ4/GKqUsj2TRtLzsBPz7p7cL1SQaY6I29xSlsPQf6IjxHSz4sDJ427GvlLQ==", encryptedElement.Element("value").Value);
            Assert.Equal("AAECAwQFBgcICQoLDA0ODw==", encryptedElement.Element("iv").Value);
            Assert.Equal("Dw4NDAsKCQgHBgUEAwIBAA==", encryptedElement.Element("key").Value);
            Assert.Equal("KeyId", encryptedElement.Element("kid").Value);
        }