示例#1
0
        /// <inheritdoc />
        internal override async Task <byte[]> GetMessageContentsAsync <T>(T data) where T : class
        {
            byte[] plaintext = await base.GetMessageContentsAsync(data).ConfigureAwait(false);

            byte[] encrypted = await EncryptionHelpers.EncryptDataAsync(plaintext, _encryptionKey).ConfigureAwait(false);

            return(encrypted);
        }
        protected override string SerializeObject(object o)
        {
            string strdata = base.SerializeObject(o);

            byte[] data      = Encoding.UTF8.GetBytes(strdata);
            byte[] encrypted = EncryptionHelpers.EncryptDataAsync(data, _encryptionKey).ConfigureAwait(false).GetAwaiter().GetResult();
            return(Convert.ToBase64String(encrypted));
        }
        protected override async Task <QueueMessage> CreateQueueMessageAsync(string content)
        {
            byte[] raw          = Encoding.UTF8.GetBytes(content);
            byte[] rawEncrypted = await EncryptionHelpers.EncryptDataAsync(raw, _encryptionKey).ConfigureAwait(false);

            string encrypted = Convert.ToBase64String(rawEncrypted);

            return(new QueueMessage(encrypted));
        }
        public async Task EncryptDataAsync_WithValidData_ReturnsEncryptedArray()
        {
            byte[] key  = EncryptionHelpers.CreateEncryptionKey();
            byte[] data = new byte[128];
            new Random().NextBytes(data);
            byte[] result = await EncryptionHelpers.EncryptDataAsync(data, key);

            Assert.NotEqual(data, result);
        }
示例#5
0
        internal override async Task <EventData> GetEventDataAsync(object message)
        {
            string serialized = JsonConvert.SerializeObject(message);

            byte[] data      = Encoding.UTF8.GetBytes(serialized);
            byte[] encrypted = await EncryptionHelpers.EncryptDataAsync(data, _encryptionKey).ConfigureAwait(false);

            return(new EventData(encrypted));
        }
        public async Task DecryptDataAsync_WithValidData_ReturnsEncryptedArray()
        {
            byte[] key       = EncryptionHelpers.CreateEncryptionKey();
            byte[] data      = new byte[128];
            byte[] encrypted = await EncryptionHelpers.EncryptDataAsync(data, key);

            byte[] result = await EncryptionHelpers.DecryptDataAsync(encrypted, key);

            Assert.Equal(data, result);
        }
示例#7
0
 internal override string GetNotificationValue(object notification)
 {
     try
     {
         string serialized = base.SerializeObject(notification);
         byte[] plaintext  = Encoding.UTF8.GetBytes(serialized);
         byte[] encrypted  = EncryptionHelpers.EncryptDataAsync(plaintext, _encryptionKey).ConfigureAwait(false).GetAwaiter().GetResult();
         return(Convert.ToBase64String(encrypted));
     }
     catch (Exception ex)
     {
         Log.Logger.Error(ex, "Failed encrypting {@Value}", notification);
         throw;
     }
 }
        public async Task EncryptingAndDecrypting_WithReadOnlyMemory_Works()
        {
            byte[] randBytes = new byte[512];
            Random rnd       = new Random();

            rnd.NextBytes(randBytes);
            ReadOnlyMemory <byte> data = new ReadOnlyMemory <byte>(randBytes);

            byte[] key       = EncryptionHelpers.CreateEncryptionKey();
            byte[] encrypted = await EncryptionHelpers.EncryptDataAsync(data, key);

            ReadOnlyMemory <byte> encryptedData = new ReadOnlyMemory <byte>(encrypted);

            byte[] decrypted = await EncryptionHelpers.DecryptDataAsync(encrypted, key);

            Assert.Equal(randBytes, decrypted);
        }
        public async Task GetDecryptionStreamAsync_WithValidData_WritesDecryptedData()
        {
            byte[] key      = EncryptionHelpers.CreateEncryptionKey();
            byte[] original = new byte[128];
            new Random().NextBytes(original);
            byte[] encrypted = await EncryptionHelpers.EncryptDataAsync(original, key);

            byte[] result;
            using (MemoryStream ms = new MemoryStream(encrypted))
                using (MemoryStream output = new MemoryStream())
                {
                    using (Stream stream = await EncryptionHelpers.GetDecryptionStreamAsync(key, ms))
                    {
                        await stream.CopyToAsync(output);
                    }

                    result = output.ToArray();
                }

            Assert.NotNull(result);
            Assert.NotEmpty(result);
            Assert.Equal(original, result);
        }
 public async Task EncryptDataAsync_WithEmptyEncryptionKey_Throws()
 {
     byte[] data = EncryptionHelpers.CreateEncryptionKey();
     await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() => await EncryptionHelpers.EncryptDataAsync(data, Array.Empty <byte>()));
 }
 public async Task EncryptDataAsync_WithNullEncryptionKey_Throws()
 {
     byte[] data = EncryptionHelpers.CreateEncryptionKey();
     await Assert.ThrowsAsync <ArgumentNullException>(async() => await EncryptionHelpers.EncryptDataAsync(data, null));
 }