Exemplo n.º 1
0
        /// <summary>
        /// MD5 hashes the specified body stream and compares it with the Content-MD5 string.
        /// </summary>
        /// <param name="contentMd5">The Content-MD5 string to compare the body hash to.</param>
        /// <param name="bodyContent">The body to hash and compare.</param>
        /// <returns><c>true</c> if equal; otherwise, <c>false</c>.</returns>
        /// <exception cref="ArgumentException">The body content stream does not support seeking or reading.</exception>
        public bool IsValidContentMd5(string contentMd5, Stream bodyContent)
        {
            if (bodyContent == null)
            {
                return(string.IsNullOrEmpty(contentMd5));
            }

            if (!bodyContent.CanSeek)
            {
                throw new ArgumentException("The body content stream does not support seeking.", nameof(bodyContent));
            }
            if (!bodyContent.CanRead)
            {
                throw new ArgumentException("The body content stream does not support reading.", nameof(bodyContent));
            }

            if (string.IsNullOrEmpty(contentMd5))
            {
                return(bodyContent.Length == 0);
            }

            string newContentMd5 = HmacSigner.CreateBase64Md5Hash(bodyContent);

            return(contentMd5 == newContentMd5);
        }
Exemplo n.º 2
0
        public void ShouldCreateBase64Md5Hash()
        {
            // Arrange
            IHmacConfiguration configuration = new HmacConfiguration {
                SignatureEncoding = "UTF-8"
            };
            HmacSigner signer = new HmacSigner(configuration, _keyRepository);

            // Act
            string base64Md5HashFromString = signer.CreateBase64Md5Hash(Body, Encoding.UTF8);
            string base64Md5HashFromBytes  = signer.CreateBase64Md5Hash(_bodyBytes);
            string base64Md5HashFromStream = signer.CreateBase64Md5Hash(_bodyStream);

            // Assert
            Assert.IsNotNull(base64Md5HashFromString);
            Assert.AreEqual(_base64Md5Hash, base64Md5HashFromString);
            Assert.IsNotNull(base64Md5HashFromBytes);
            Assert.AreEqual(_base64Md5Hash, base64Md5HashFromBytes);
            Assert.IsNotNull(base64Md5HashFromStream);
            Assert.AreEqual(_base64Md5Hash, base64Md5HashFromStream);
        }
Exemplo n.º 3
0
        /// <summary>
        /// MD5 hashes the specified body byte array and compares it with the Content-MD5 string.
        /// </summary>
        /// <param name="contentMd5">The Content-MD5 string to compare the body hash to.</param>
        /// <param name="bodyContent">The body to hash and compare.</param>
        /// <returns><c>true</c> if equal; otherwise, <c>false</c>.</returns>
        public bool IsValidContentMd5(string contentMd5, byte[] bodyContent)
        {
            if (string.IsNullOrEmpty(contentMd5))
            {
                return(bodyContent.IsNullOrEmpty());
            }
            if (bodyContent.IsNullOrEmpty())
            {
                return(false);
            }

            string newContentMd5 = HmacSigner.CreateBase64Md5Hash(bodyContent);

            return(contentMd5 == newContentMd5);
        }
Exemplo n.º 4
0
        /// <summary>
        /// MD5 hashes the specified body and compares it with the Content-MD5 string.
        /// </summary>
        /// <param name="contentMd5">The Content-MD5 string to compare the body hash to.</param>
        /// <param name="bodyContent">The body to hash and compare.</param>
        /// <param name="encoding">The encoding to use when converting the body content into bytes.</param>
        /// <returns><c>true</c> if equal; otherwise, <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException">The encoding is null.</exception>
        public bool IsValidContentMd5(string contentMd5, string bodyContent, Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding), "The encoding cannot be null.");
            }

            if (string.IsNullOrEmpty(contentMd5))
            {
                return(string.IsNullOrEmpty(bodyContent));
            }

            if (string.IsNullOrEmpty(bodyContent))
            {
                return(false);
            }

            string newContentMd5 = HmacSigner.CreateBase64Md5Hash(bodyContent, encoding);

            return(contentMd5 == newContentMd5);
        }