Exemplo n.º 1
0
        public string GetHash(HashType hashType)
        {
            CheckInternalState();

            string     output = string.Empty;
            IHashMaker hasher = new HashMaker();

            switch (hashType)
            {
            case HashType.MD5:
                if (Settings.Default.HoldBufferInMemory)
                {
                    output = hasher.GetMD5(_InternalBuffer);
                }
                else
                {
                    output = hasher.GetMD5(_InternalFilePath);
                }
                break;

            case HashType.SHA1:
                if (Settings.Default.HoldBufferInMemory)
                {
                    output = hasher.GetSHA1(_InternalBuffer);
                }
                else
                {
                    output = hasher.GetSHA1(_InternalFilePath);
                }
                break;

            case HashType.SHA256:
                if (Settings.Default.HoldBufferInMemory)
                {
                    output = hasher.GetSHA256(_InternalBuffer);
                }
                else
                {
                    output = hasher.GetSHA256(_InternalFilePath);
                }
                break;

            case HashType.PreHash:
                output = hasher.GetPreHash(_InternalFilePath);
                break;
            }

            return(output);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int totalProcessed = 0;
            int totalHashed    = 0;
            int totalFailed    = 0;


            IEnumerable <FilesDueForHash_Result> filesToHash = GetNextFiles();

            do
            {
                Parallel.ForEach(filesToHash, fileToHash =>
                {
                    Console.WriteLine("Processing: " + fileToHash.FullPath);

                    try
                    {
                        if (File.Exists(fileToHash.FullPath) &&
                            fileToHash.Length > 0)
                        {
                            Stream fileStream = File.OpenRead(fileToHash.FullPath);

                            IHashMaker hasher = new HashMaker();

                            string preHash = hasher.GetPreHash(fileToHash.FullPath);
                            string md5     = hasher.GetMD5(fileStream);
                            string sha1    = hasher.GetSHA1(fileStream);

                            using (DbModelContainer db = new DbModelContainer())
                            {
                                db.Database.CommandTimeout = 60;

                                TrackedFile fileToUpdate = db.TrackedFiles.Find(fileToHash.Id);
                                fileToUpdate.PreHash     = preHash;
                                fileToUpdate.MD5         = md5;
                                fileToUpdate.SHA1        = sha1;

                                db.SaveChanges();
                            }

                            Interlocked.Increment(ref totalHashed);
                        }
                    }
                    catch (Exception)
                    {
                        //mark it as failed
                        Interlocked.Increment(ref totalFailed);
                    }
                    finally
                    {
                        Interlocked.Increment(ref totalProcessed);

                        using (DbModelContainer db = new DbModelContainer())
                        {
                            db.Database.CommandTimeout = 60;
                            TrackedFile file           = db.TrackedFiles.Find(fileToHash.Id);
                            file.HashAttempted         = DateTime.Now;
                            db.SaveChanges();
                        }
                    }
                });

                filesToHash = GetNextFiles();
            } while (filesToHash.Count() > 0);
        }