Ejemplo n.º 1
0
        private void TestEnvelopeEncrypt()
        {
            byte[] expectedEncryptedKey = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
            byte[] expectedPlainText    = { 1, 2, 3, 4, 5 };
            byte[] expectedCipherText   = { 5, 4, 3, 2, 1 };

            aeadEnvelopeCryptoMock.Setup(x => x.Encrypt(expectedPlainText, keyMock.Object)).Returns(expectedCipherText);
            aeadEnvelopeCryptoMock.Setup(x => x.EncryptKey(keyMock.Object, keyEncryptionKey.Object))
            .Returns(expectedEncryptedKey);
            aeadEnvelopeCryptoMock.Setup(x => x.GenerateKey()).Returns(keyMock.Object);
            aeadEnvelopeCryptoMock.Setup(x => x.EnvelopeEncrypt(expectedPlainText, keyEncryptionKey.Object, null))
            .CallBase();

            EnvelopeEncryptResult result = aeadEnvelopeCryptoMock.Object.EnvelopeEncrypt(expectedPlainText, keyEncryptionKey.Object, null);

            Assert.Equal(expectedCipherText, result.CipherText);
            Assert.Equal(expectedEncryptedKey, result.EncryptedKey);
            Assert.Null(result.UserState);
            keyMock.Verify(x => x.Dispose());
        }
        /// <inheritdoc/>
        public virtual JObject EncryptPayload(byte[] payload)
        {
            using (MetricsUtil.MetricsInstance.Measure.Timer.Time(EncryptTimerOptions))
            {
                EnvelopeEncryptResult result = WithIntermediateKeyForWrite(intermediateCryptoKey => crypto.EnvelopeEncrypt(
                                                                               payload,
                                                                               intermediateCryptoKey,
                                                                               new KeyMeta(partition.IntermediateKeyId, intermediateCryptoKey.GetCreated())));

                KeyMeta parentKeyMeta = (KeyMeta)result.UserState;

                EnvelopeKeyRecord keyRecord =
                    new EnvelopeKeyRecord(DateTimeOffset.UtcNow, parentKeyMeta, result.EncryptedKey);

                Json wrapperDocument = new Json();
                wrapperDocument.Put("Key", keyRecord.ToJson());
                wrapperDocument.Put("Data", result.CipherText);

                return(wrapperDocument.ToJObject());
            }
        }