Exemple #1
0
        public static string GetHash(string path, Hash hashType, bool log = true, bool quick = true)
        {
            Benchmarker.Start();
            string hashStr = "";

            if (IsPathDirectory(path))
            {
                Logger.Log($"Path '{path}' is directory! Returning empty hash.", true);
                return(hashStr);
            }
            try
            {
                var stream = File.OpenRead(path);

                if (hashType == Hash.MD5)
                {
                    MD5 md5  = MD5.Create();
                    var hash = md5.ComputeHash(stream);
                    hashStr = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
                }

                if (hashType == Hash.CRC32)
                {
                    var crc        = new Crc32Algorithm();
                    var crc32bytes = crc.ComputeHash(stream);
                    hashStr = BitConverter.ToUInt32(crc32bytes, 0).ToString();
                }

                if (hashType == Hash.xxHash)
                {
                    ulong xxh64 = xxHash64.ComputeHash(stream, 8192, (ulong)GetFilesize(path));
                    hashStr = xxh64.ToString();
                }

                stream.Close();
            }
            catch (Exception e)
            {
                Logger.Log($"Error getting file hash for {Path.GetFileName(path)}: {e.Message}", true);
                return("");
            }
            if (log)
            {
                Logger.Log($"Computed {hashType} for '{Path.GetFileNameWithoutExtension(path).Trunc(40) + Path.GetExtension(path)}' ({GetFilesizeStr(path)}) in {Benchmarker.GetTimeStr(true)}: {hashStr}", true);
            }
            return(hashStr);
        }