示例#1
0
        public int XxHash()
        {
            int a = 0;

            if (TestType == Types.ShortSentence)
            {
                for (int i = 0; i < _sentences.GetLength(0); i++)
                {
                    _xxHash.ComputeHash(_sentences[i]);
                    a++;
                }
            }
            if (TestType == Types.Word)
            {
                for (int i = 0; i < _words.GetLength(0); i++)
                {
                    _xxHash.ComputeHash(_words[i]);
                    a++;
                }
            }
            if (TestType == Types.LongText)
            {
                _xxHash.ComputeHash(_longText);
            }
            return(a);
        }
示例#2
0
 public static string HashValue(string value)
 {
     if (value != null || !String.IsNullOrEmpty(value))
     {
         var hash = xxHash.ComputeHash(value);
         return(hash.AsBase64String());
     }
     return(string.Empty);
 }
        public async Task <string> GetFileHash(string content)
        {
            var fileHash = _xxHashInstance.ComputeHash(Convert.FromBase64String(content)).AsBase64String();
            var fileId   = await _repository.GetAttachmentIdByHash(fileHash).ConfigureAwait(false);

            if (fileId != default(Guid))
            {
                return(fileHash);
            }

            return(null);
        }
示例#4
0
        /// <summary>
        /// Get a 32bit xxHash of an image (useful for comparing)
        /// </summary>
        public static byte[] XxHash(this Image image)
        {
            var bytes = new byte[1];

            bytes = (byte[])new ImageConverter().ConvertTo(image, bytes.GetType());
            return(XxHashFactory.ComputeHash(bytes).Hash);
        }
        private string GetCacheKey(Expression query)
        {
            var resultQuery = _queryModelGenerator.ParseQuery(query);
            var hashOfQuery = _xxHash.ComputeHash(Encoding.UTF8.GetBytes(resultQuery.ToString()));

            return(hashOfQuery.AsBase64String());
        }
示例#6
0
 public static string ComputeHashSync(this string text)
 {
     using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
     {
         var value = HashFunction.ComputeHash(stream);
         return(value.AsBase64String());
     }
 }
 /// <summary>
 /// Calculate the hash of a file with buffered read
 /// </summary>
 public static async Task <string> GetFileHash(string path)
 {
     using (var stream = new BufferedStream(File.OpenRead(path), 1200000))
     {
         byte[] hashedValue = XxHashFactory.ComputeHash(stream).Hash;
         return(await Task.FromResult(BitConverter.ToString(hashedValue)));
     }
 }
示例#8
0
        private void ComputeXxHash()
        {
            IxxHash xxHash = xxHashFactory.Instance.Create();

            byte[]     file      = File.ReadAllBytes(Path);
            IHashValue hashValue = xxHash.ComputeHash(file);

            Hash = hashValue.AsHexString(false);
        }
        public static string ComputeHashSync(this object obj)
        {
            var serialized = JsonConvert.SerializeObject(obj);

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(serialized)))
            {
                var value = HashFunction.ComputeHash(stream);
                return(value.AsBase64String());
            }
        }
示例#10
0
        public uint XxHash()
        {
            uint result = 0;

            foreach (var bytes in _byteArrays)
            {
                var hash = _xxHash.ComputeHash(bytes).Hash;
                result += BitConverter.ToUInt32(hash);
            }

            return(result);
        }
        public uint XxHash()
        {
            uint result = 0;

            foreach (var str in _strings)
            {
                var hash = _xxHash.ComputeHash(str).Hash;
                result += BitConverter.ToUInt32(hash);
            }

            return(result);
        }
示例#12
0
 /// <summary>
 ///     Compute xxhash for string and return hex value
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static string ComputeHash(string value)
 {
     return(Instance.ComputeHash(Encoding.UTF8.GetBytes(value)).AsHexString());
 }
示例#13
0
        /// <summary>
        /// xxHash(64) is supposed to be quite fast
        /// </summary>
        public override void ComputeModifiedFilesList(int reportCount = 100)
        {
            CommonFilesSourceHash = new string[CommonFiles.Length];
            string targetHashFilePath = Path.Combine(TargetPath, "blenny_backup_hash.txt");
            Dictionary <string, string> targetHashRef = new Dictionary <string, string>();

            if (File.Exists(targetHashFilePath))
            {
                targetHashRef = GetHashFromFile(targetHashFilePath);
            }

            ConcurrentStack <string> modifiedFiles = new ConcurrentStack <string>();

            int k         = 0;
            int consoleTh = CommonFiles.Length / reportCount;

            Parallel.For(0, CommonFiles.Length, new ParallelOptions()
            {
                MaxDegreeOfParallelism = Program.MaxDegreeOfParallelism
            }, i =>
            {
                string SourceHash = "";
                string TargetHash = "";

                // read https://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net
                try
                {
                    using (var fs = new FileStream(Path.Combine(SourcePath, CommonFiles[i]), FileMode.Open, FileAccess.Read, FileShare.Read))
                        SourceHash = ixxHash.ComputeHash(fs).AsHexString();

                    if (targetHashRef.ContainsKey(CommonFiles[i]))
                    {
                        TargetHash = targetHashRef[CommonFiles[i]];
                    }
                    else
                    {
                        using (var fs = new FileStream(Path.Combine(TargetPath, CommonFiles[i]), FileMode.Open, FileAccess.Read, FileShare.Read))
                            TargetHash = ixxHash.ComputeHash(fs).AsHexString();
                    }

                    int progress = System.Threading.Interlocked.Increment(ref k);
                    ProgressReporter.Logger.WriteLine("Processed hash for " + progress + " / " + CommonFiles.Length + " files", Logging.LogLevel.File);
                    if (consoleTh == 0 || k % consoleTh == 0)
                    {
                        ProgressReporter.Logger.WriteLine("Processed hash for " + progress + " / " + CommonFiles.Length + " files", Logging.LogLevel.Console);
                    }
                }
                catch (Exception e)
                {
                    ProgressReporter.Logger.WriteLine("\r\nERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR\r\nERROR : " + e.ToString() + "\r\nERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR\r\n");
                }

                if (SourceHash != TargetHash)
                {
                    modifiedFiles.Push(CommonFiles[i]);
                }

                CommonFilesSourceHash[i] = SourceHash + "|" + CommonFiles[i];
            });
            ModifiedFiles = modifiedFiles.ToArray();
        }
示例#14
0
        /// <summary>
        /// Returns a 64bit hash in hex of supplied key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ComputeHash(string key)
        {
            var hash = _hasher.ComputeHash(key, 64);

            return(hash.AsHexString());
        }
示例#15
0
 public byte[] ComputeHash(Stream stream)
 {
     return(algorithm.ComputeHash(stream).Hash);
 }