void IMessageSerializer.Serialize <T>(Stream stream, SendContext <T> context) { context.ContentType = EncryptedContentType; var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames); using Stream cryptoStream = _streamProvider.GetEncryptStream(stream, context); using var jsonWriter = new BsonDataWriter(cryptoStream); _serializer.Serialize(jsonWriter, envelope, typeof(MessageEnvelope)); jsonWriter.Flush(); }
/// <summary> /// Returns a stream with the encryption bits in place to ensure proper message encryption /// </summary> /// <param name="provider">The crypto stream provider</param> /// <param name="stream">The original stream to which the encrypted message content is written</param> /// <param name="context">The second context of the message</param> /// <returns>A stream for serializing the message which will be encrypted</returns> public static Stream GetEncryptStream(this ICryptoStreamProvider provider, Stream stream, TransformContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var keyId = context.Headers.TryGetHeader(EncryptedMessageSerializer.EncryptionKeyHeader, out var keyIdObj) ? keyIdObj.ToString() : default; return(provider.GetEncryptStream(stream, keyId, CryptoStreamMode.Write)); }
public async Task <Uri> Put(Stream stream, TimeSpan?timeToLive = null, CancellationToken cancellationToken = new CancellationToken()) { string keyId = null; using (Stream cryptoStream = _streamProvider.GetEncryptStream(stream, keyId, CryptoStreamMode.Read)) { var address = await _repository.Put(cryptoStream, timeToLive, cancellationToken); var addressBuilder = new UriBuilder(address); NameValueCollection parameters = new NameValueCollection(); if (!string.IsNullOrWhiteSpace(addressBuilder.Query)) { var query = addressBuilder.Query; if (query.Contains("?")) { query = query.Substring(query.IndexOf('?') + 1); } foreach (string parameter in query.Split('&')) { if (string.IsNullOrWhiteSpace(parameter)) { continue; } string[] pair = parameter.Split('='); parameters.Add(pair[0], pair.Length == 2 ? pair[1] : ""); } } parameters["keyId"] = ""; addressBuilder.Query = parameters.ToString(); return(addressBuilder.Uri); } }