Exemplo n.º 1
0
        //**************************************************
        //* Private
        //**************************************************

        //-------------------------------------------------
        /// <summary>
        /// Find hash algorithm that corresponds to the
        /// requested method
        /// </summary>
        /// <exception cref="NotSupportedException">Thrown
        /// when requested method is not recognized.</exception>
        private static HashAlgorithm CreateHashAlgorithm(ChecksumMethod method)
        {
            switch (method)
            {
            case ChecksumMethod.SHA1: return(new SHA1CryptoServiceProvider());

            case ChecksumMethod.MD5: return(new MD5CryptoServiceProvider());

            case ChecksumMethod.CRC32: return(new CRC32());

            default: throw new NotSupportedException(string.Concat("Requested checksum method ", method, " is not supported"));
            }
        }
Exemplo n.º 2
0
        //**************************************************
        //* Public interface
        //**************************************************

        //-------------------------------------------------
        /// <summary>
        /// Compute checksum for the specified source file,
        /// return it as hex string ("F3BA87...").
        /// </summary>
        /// <remarks>
        /// This method will be run on a worker thread
        /// (from thread pool) via async invocation.
        /// </remarks>
        public static string ComputeChecksum(string sourceFile, ChecksumMethod method, Action <decimal> progressNotifier)
        {
            System.Threading.Thread.Sleep(100);
            using (var hashAlgorithm = CreateHashAlgorithm(method))
                using (var source = new TrackingStream(File.OpenRead(sourceFile), progressNotifier))
                {
                    var hash = hashAlgorithm.ComputeHash(source);

                    var msg = new StringBuilder(128);
                    foreach (var byteValue in hash)
                    {
                        msg.AppendFormat(byteValue.ToString("X2", CultureInfo.InvariantCulture));
                    }
                    return(msg.ToString());
                }
        }
Exemplo n.º 3
0
 protected ENetChecksum(ChecksumMethod method)
 {
     Method = method;
 }