Example #1
0
File: Program.cs Project: fdsc/old
        public readonly static string MainMutexName; // = "relaxtime.8vs.ru|" + BitConverter.ToString(System.Text.Encoding.Unicode.GetBytes(Application.StartupPath)).Replace("-", "");

        static Program()
        {
            var sha = new keccak.SHA3(1024);

            MainMutexName = "relaxtime.8vs.ru|" + BitConverter.ToString(sha.getHash224(System.Text.Encoding.Unicode.GetBytes(AppDomain.CurrentDomain.BaseDirectory))).Replace("-", "");
            m             = new System.Threading.Semaphore(1, 1, MainMutexName);
        }
Example #2
0
        private byte[] GetHash224(string fileName)
        {
            var fl = new FileInfo(fileName).Length;

            if (fl < 72 * 1024 * 1024)
            {
                var data = File.ReadAllBytes(fileName);
                return(new keccak.SHA3(data.Length).getHash224(data));
            }

            VisibleProgressBar();

            var buff = new byte[72 * 1024 * 1024];
            var hash = new byte[64];
            var sha3 = new keccak.SHA3(buff.Length);

            using (var or = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 0, FileOptions.WriteThrough))
            {
                int  tmp    = 0;
                long all    = 0;
                bool isInit = false;
                do
                {
                    tmp  = or.Read(buff, 0, buff.Length);
                    all += tmp;
                    sha3.getHash224(buff, tmp, isInit, tmp != buff.Length, hash);
                    isInit = true;
                    ProgressToProgressBar(fl, all);
                }while (tmp > 0 && tmp == buff.Length);
            }

            ProgressBarUnvisible();
            return(hash);
        }
Example #3
0
        private void CalcFileHashes(List <fileNameAndStream> list)
        {
            toCalc = list.Count;

            this.Invoke
            (
                new VOID
                (
                    delegate
            {
                lock (sync)
                {
                    logBox.AppendText("\r\nВсего добавлено " + list.Count);
                    if (addErrors > 0)
                    {
                        logBox.AppendText("\r\nОшибок при добавлении " + addErrors);
                    }

                    logBox.AppendText("\r\n\t" + DateTime.Now.ToLongTimeString());

                    logBox.AppendText("\r\n");
                    logBox.AppendText("\r\nНачинаются вычисления. Результат добавляется в файл " + fName);
                }
            }
                )
            );

            loopCnt = 0;
            int procCount = 0;

            for (var i = 0; i < 1 /*Environment.ProcessorCount + 1*/; i++)
            {
                var procNum = i;
                System.Threading.ThreadPool.QueueUserWorkItem
                (
                    delegate
                {
                    Interlocked.Increment(ref procCount);
                    try
                    {
                        int len;
                        if (hashType == "512")
                        {
                            len = keccak.SHA3.rNumbers[3] >> 3;
                        }
                        else
                        if (hashType == "384")
                        {
                            len = keccak.SHA3.rNumbers[2] >> 3;
                        }
                        else
                        if (hashType == "256")
                        {
                            len = keccak.SHA3.rNumbers[1] >> 3;
                        }
                        else
                        {
                            len = keccak.SHA3.rNumbers[0] >> 3;
                        }

                        var a = new keccak.SHA3(len + 1024);

                        string FileName = "";
                        fileNameAndStream fns;
                        int lastNum = 0;
                        // FileInfo fi = null;
                        byte[] buff;

                        /*if (procNum > 0)
                         *  buff = new byte[64*1024*1024];
                         * else*/
                        buff        = new byte[len];
                        byte[] hash = new byte[64];

                        Start:
                        FileName = "";
                        long aft = 0, act = 0;
                        long n1;
                        try
                        {
                            long fl;
                            lock (sync)
                            {
                                lastNum = list.Count - 1;

                                /*File:
                                 * fsi = null;
                                 * fi  = null;
                                 * di  = null;*/
                                if (toCalc <= 0 || (lastNum < 0 && procNum > 0))
                                {
                                    return;
                                }

                                //if (lastNum >= list.Count)
                                if (list.Count <= 0)
                                {
                                    while (procCount > 1)
                                    {
                                        Monitor.Wait(sync);
                                    }

                                    if (procNum == 0)
                                    {
                                        foreach (var afh in lfh)
                                        {
                                            File.AppendAllText(fName, afh.FileName + "\r\n" + afh.hash + "\r\n");
                                        }

                                        lfh.Clear();
                                    }
                                    return;
                                }

                                fns      = list[lastNum];
                                FileName = fns.fileName + fns.fileStream;
                                n1       = DateTime.Now.Ticks;

                                fl = fns.size;

                                /*fi = new FileInfo(fns.fileName);
                                 *
                                 * fl = 0;
                                 * if (fi.Exists)
                                 * {
                                 *  fl = fi.Length;
                                 *  fsi = fi;
                                 * }
                                 * else
                                 * {
                                 *  di = new DirectoryInfo(FileName);
                                 *  if (di.Exists)
                                 *  {
                                 *      fsi = di;
                                 *  }
                                 * }*/
                                aft += DateTime.Now.Ticks - n1;

                                /*if (fl > buff.Length)
                                 * // if (procNum > 0 || fl > 1024 * 1024 * 256)
                                 * {
                                 *  // if (fl > 1024 * 1024 * 256)
                                 *  //lock (sync)
                                 *  {
                                 *      n1 = DateTime.Now.Ticks;
                                 *      File.AppendAllText(fName, FileName + "\r\n" + "skipped " + fl + "\r\n");
                                 *      aft += DateTime.Now.Ticks - n1;
                                 *
                                 *      n1 = DateTime.Now.Ticks;
                                 *      list.RemoveAt(lastNum);
                                 *      act += DateTime.Now.Ticks - n1;
                                 *      calcCountS++;
                                 *  }
                                 *
                                 *  lastNum--;
                                 *  goto File;
                                 * }*/

                                n1 = DateTime.Now.Ticks;
                                list.RemoveAt(lastNum);
                                act += DateTime.Now.Ticks - n1;

                                /*
                                 * if (fsi == null)
                                 *  goto Start;*/
                            }

                            /*
                             * if (di != null)
                             * {
                             *  var dac = di.GetAccessControl();
                             *
                             *  var sb  = new StringBuilder();
                             *
                             *  // new FileIOPermission(FileIOPermissionAccess.Read, di.FullName).Demand();
                             *
                             *  var dars = dac.GetAccessRules(true, false, WindowsIdentity.GetCurrent().GetType());
                             *  foreach (var dar in dars)
                             *  {
                             *
                             *      sb.AppendLine();
                             *  }
                             *
                             *  di = null;
                             *  goto Start;
                             * }
                             */
                            // var fac = fi.GetAccessControl();

                            bool isInit = false;
                            int tmp     = 0;
                            // byte[] fc = null;
                            // Эта блокировка нужна только для того, чтобы к диску потоки обращались последовательно
                            try
                            {
                                lock (list)
                                {
                                    // fc = File.ReadAllBytes(FileName);

                                    n1 = DateTime.Now.Ticks;

                                    var fnb = utf16.GetBytes(FileName);
                                    int bin = Form1.CreateFileW(fnb, 0x80000000, 0x00000002 | 0x00000001, 0, 3, 0x20000000 | 0x80000000, 0);     // FILE_FLAG_NO_BUFFERING FILE_FLAG_WRITE_THROUGH
                                    if (bin <= 0)
                                    {
                                        throw new Exception("CreateFileW in readFile GetLast error " + Form1.GetLastError() + " for file " + FileName);
                                    }

                                    try
                                    {
                                        aft += DateTime.Now.Ticks - n1;

                                        do
                                        {
                                            n1 = DateTime.Now.Ticks;
                                            FormCrypt.ReadFile(bin, buff, buff.Length, out tmp, 0);
                                            if (hash == null && tmp <= 0)
                                            {
                                                throw new Exception("ReadFile in readFile GetLast error " + Form1.GetLastError() + " for file " + FileName);
                                            }

                                            aft += DateTime.Now.Ticks - n1;


                                            n1 = DateTime.Now.Ticks;
                                            if (hashType == "512")
                                            {
                                                hash = a.getHash512(buff, tmp, isInit, tmp != buff.Length, hash);
                                            }
                                            else
                                            if (hashType == "384")
                                            {
                                                hash = a.getHash384(buff, tmp, isInit, tmp != buff.Length, hash);
                                            }
                                            else
                                            if (hashType == "256")
                                            {
                                                hash = a.getHash256(buff, tmp, isInit, tmp != buff.Length, hash);
                                            }
                                            else
                                            {
                                                hash = a.getHash224(buff, tmp, isInit, tmp != buff.Length, hash);
                                            }

                                            isInit = true;
                                            act   += DateTime.Now.Ticks - n1;
                                        }while (tmp > 0 && tmp == buff.Length);
                                    }
                                    finally
                                    {
                                        Form1.CloseHandle(bin);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                lock (sync)
                                {
                                    calcCountE++;
                                }

                                lock (sync)
                                {
                                    try
                                    {
                                        File.AppendAllText(fName, FileName + "\r\n" + "error " + ex.Message + "\r\n");
                                    }
                                    catch
                                    {}
                                }

                                goto Start;
                            }

                            n1     = DateTime.Now.Ticks;
                            var fh = new FileHash(FileName, Convert.ToBase64String(hash));

                            lock (sync)
                            {
                                allFTime += aft;

                                lfh.Add(fh);
                                act      += DateTime.Now.Ticks - n1;
                                allCTime += act;

                                calcCount++;
                            }
                        }
                        catch (Exception ex)
                        {
                            lock (sync)
                            {
                                calcCountE++;
                            }

                            lock (sync)
                            {
                                try
                                {
                                    File.AppendAllText(fName, FileName + "\r\n" + "error " + ex.Message + "\r\n");
                                }
                                catch
                                {}
                            }

                            try
                            {
                                this.Invoke
                                (
                                    new VOID
                                    (
                                        delegate
                                {
                                    lock (sync)
                                    {
                                        logBox.AppendText("\r\nОшибка " + ex.Message);
                                        logBox.AppendText("\r\nОшибка " + FileName);
                                    }
                                }
                                    )
                                );
                            }
                            catch
                            {}
                        }

                        goto Start;
                    }
                    finally
                    {
                        Interlocked.Decrement(ref procCount);
                        lock (sync)
                            Monitor.Pulse(sync);
                    }
                }
                );
            }
        }