Пример #1
0
        /// <summary>
        /// Serialize the HTTP content to a stream as an asynchronous operation.
        /// </summary>
        /// <param name="stream">The target stream.</param>
        /// <param name="context">Information about the transport (channel binding token, for example). This parameter may be null.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            if (!_contentToBeDecrypted.Headers.ContentEncoding.Contains(Constants.ENCRYPTED_CONTENT_ENCODING))
            {
                throw new NotSupportedException($"Encryption type not supported or stream isn't encrypted. The only sypported encryption type is '{Constants.ENCRYPTED_CONTENT_ENCODING}'.");
            }

            Stream streamToBeDecrypted = await _contentToBeDecrypted.ReadAsStreamAsync().ConfigureAwait(false);

            await Aes128GcmEncoding.DecodeAsync(streamToBeDecrypted, stream, _keyLocator).ConfigureAwait(false);
        }
        /// <summary>
        /// Determines whether the HTTP content has a valid length in bytes.
        /// </summary>
        /// <param name="length">The length in bytes of the HTTP content.</param>
        /// <returns>True if length is a valid length, otherwise false.</returns>
        protected override bool TryComputeLength(out long length)
        {
            length = 0;
            bool hasValidLength = false;

            if (_length.HasValue)
            {
                length         = _length.Value;
                hasValidLength = true;
            }
            else
            {
                HttpContentTryComputeLengthDelegate httpContentTryComputeLengthDelegateInstance = (HttpContentTryComputeLengthDelegate)_httpContentTryComputeLengthMethodInfo.CreateDelegate(_httpContentTryComputeLengthDelegateType, _contentToBeEncrypted);

                if (httpContentTryComputeLengthDelegateInstance(out long sourceLength))
                {
                    _length        = length = Aes128GcmEncoding.ComputeEncodedLength(sourceLength, (byte)(_keyId?.Length ?? 0), _recordSize);
                    hasValidLength = true;
                }
            }

            return(hasValidLength);
        }
        /// <summary>
        /// Serialize the HTTP content to a stream as an asynchronous operation.
        /// </summary>
        /// <param name="stream">The target stream.</param>
        /// <param name="context">Information about the transport (channel binding token, for example). This parameter may be null.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            Stream streamToBeEncrypted = await _contentToBeEncrypted.ReadAsStreamAsync().ConfigureAwait(false);

            await Aes128GcmEncoding.EncodeAsync(streamToBeEncrypted, stream, _key, _keyId, _recordSize).ConfigureAwait(false);
        }
 /// <summary>
 /// Instantiates a new <see cref="Aes128GcmDecodedContent"/>.
 /// </summary>
 /// <param name="contentToBeDecrypted">The content which will be decoded.</param>
 /// <param name="keyProvider">The function which is able to provide the keying material based on the keying material identificator.</param>
 public Aes128GcmDecodedContent(HttpContent contentToBeDecrypted, Func <string, byte[]> keyProvider)
     : this(contentToBeDecrypted, Aes128GcmEncoding.ConvertToByteArrayBasedKeyProvider(keyProvider))
 {
 }
Пример #5
0
 /// <summary>
 /// Instantiates a new <see cref="Aes128GcmEncodingHandler"/>.
 /// </summary>
 /// <param name="keyProvider">The function which is able to provide the keying material based on the keying material identificator.</param>
 public Aes128GcmEncodingHandler(Func <string, byte[]> keyProvider)
     : this(Aes128GcmEncoding.ConvertToByteArrayBasedKeyProvider(keyProvider))
 {
 }