Esempio n. 1
0
        /// <summary>
        /// Return the MD5 hash of the provided memory stream as a string. Stream position will be equal to the length of stream on
        /// return, this ensures the MD5 is consistent.
        /// </summary>
        /// <param name="streamToMD5">The bytes which will be checksummed</param>
        /// <returns>The MD5 checksum as a string</returns>
        public static string MD5(Stream streamToMD5)
        {
            if (streamToMD5 == null)
            {
                throw new ArgumentNullException("streamToMD5", "Provided Stream cannot be null.");
            }

            string resultStr;

#if NETFX_CORE
            var alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5);
            var buffer = (new Windows.Storage.Streams.DataReader(streamToMD5.AsInputStream())).ReadBuffer((uint)streamToMD5.Length);
            var hashedData = alg.HashData(buffer);
            resultStr = Windows.Security.Cryptography.CryptographicBuffer.EncodeToHexString(hashedData).Replace("-", "");
#else
            using (System.Security.Cryptography.HashAlgorithm md5 =
#if WINDOWS_PHONE
                       new Tools.MD5Managed())
#else
                       System.Security.Cryptography.MD5.Create())
#endif
            {
                //If we don't ensure the position is consistent the MD5 changes
                streamToMD5.Seek(0, SeekOrigin.Begin);
                resultStr = BitConverter.ToString(md5.ComputeHash(streamToMD5)).Replace("-", "");
            }
#endif
            return(resultStr);
        }
Esempio n. 2
0
            /// <summary>
            /// Return the MD5 for the specific part of the stream only.
            /// </summary>
            /// <returns></returns>
            public string MD5CheckSum()
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    ThreadSafeStream.CopyTo(ms, Start, Length, 8000);

#if NETFX_CORE
                    var alg        = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5);
                    var buffer     = (new Windows.Storage.Streams.DataReader(ms.AsInputStream())).ReadBuffer((uint)ms.Length);
                    var hashedData = alg.HashData(buffer);
                    return(Windows.Security.Cryptography.CryptographicBuffer.EncodeToHexString(hashedData).Replace("-", ""));
#else
#if WINDOWS_PHONE
                    using (var md5 = new Tools.MD5Managed())
                    {
#else
                    using (var md5 = System.Security.Cryptography.MD5.Create())
                    {
#endif
                        return(BitConverter.ToString(md5.ComputeHash(ms)).Replace("-", ""));
                    }
#endif
                }
            }
            /// <summary>
            /// Return the MD5 for the specific part of the stream only.
            /// </summary>
            /// <returns></returns>
            public string MD5CheckSum()
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    ThreadSafeStream.CopyTo(ms, Start, Length, 8000);

#if NETFX_CORE
                var alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5);
                var buffer = (new Windows.Storage.Streams.DataReader(ms.AsInputStream())).ReadBuffer((uint)ms.Length);
                var hashedData = alg.HashData(buffer);
                return Windows.Security.Cryptography.CryptographicBuffer.EncodeToHexString(hashedData).Replace("-", "");
#else
#if WINDOWS_PHONE
                using(var md5 = new Tools.MD5Managed())
                {
#else
                    using (var md5 = System.Security.Cryptography.MD5.Create())
                    {
#endif
                        return BitConverter.ToString(md5.ComputeHash(ms)).Replace("-", "");
                    }
#endif
                }
            }
        /// <summary>
        /// Return the MD5 hash of the provided memory stream as a string. Stream position will be equal to the length of stream on 
        /// return, this ensures the MD5 is consistent.
        /// </summary>
        /// <param name="streamToMD5">The bytes which will be checksummed</param>
        /// <returns>The MD5 checksum as a string</returns>
        public static string MD5(Stream streamToMD5)
        {
            if (streamToMD5 == null) throw new ArgumentNullException("streamToMD5", "Provided Stream cannot be null.");

            string resultStr;

#if NETFX_CORE
            var alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5);
            var buffer = (new Windows.Storage.Streams.DataReader(streamToMD5.AsInputStream())).ReadBuffer((uint)streamToMD5.Length);
            var hashedData = alg.HashData(buffer);
            resultStr = Windows.Security.Cryptography.CryptographicBuffer.EncodeToHexString(hashedData).Replace("-", "");
#else
            using (System.Security.Cryptography.HashAlgorithm md5 =
#if WINDOWS_PHONE
                new Tools.MD5Managed())
#else
 System.Security.Cryptography.MD5.Create())
#endif
            {
                //If we don't ensure the position is consistent the MD5 changes
                streamToMD5.Seek(0, SeekOrigin.Begin);
                resultStr = BitConverter.ToString(md5.ComputeHash(streamToMD5)).Replace("-", "");
            }
#endif
            return resultStr;
        }