示例#1
0
        /// <summary>
        /// Get a name for mutex base on the path.
        /// </summary>
        /// <param name="path">Custom file or directory path.</param>
        /// <param name="isGlobal">The name is for global mutex?</param>
        /// <returns>A name for mutex base on the path.</returns>
        public static string GetMutexNameForPath(string path, bool isGlobal = true)
        {
            var fullPath = Path.GetFullPath(path);
            var pathMd5  = MD5CryptoUtility.ComputeHash(fullPath);

            return(GetMutexName(pathMd5, isGlobal));
        }
示例#2
0
        public void ComputeHashTest()
        {
            var buffer = new byte[] { 0, 1, 2 };
            var hash   = MD5CryptoUtility.ComputeHash(buffer);

            Assert.IsNotNull(hash);
            Console.WriteLine(hash);
        }
示例#3
0
        public void ComputeFileHashTest()
        {
            var filePath = string.Format("{0}/MGS.Common.xml", Environment.CurrentDirectory);
            var hash     = MD5CryptoUtility.ComputeFileHash(filePath);

            Assert.IsNotNull(hash);
            Console.WriteLine(hash);
        }
示例#4
0
        public void ComputeHashTest1()
        {
            var source = "ComputeHashTest1";
            var hash   = MD5CryptoUtility.ComputeHash(source);

            Assert.IsNotNull(hash);
            Console.WriteLine(hash);
        }
示例#5
0
        /// <summary>
        /// Copy the file in to cache.
        /// </summary>
        /// <param name="file">The target file path.</param>
        /// <param name="key">Key for cache file [Use md5 hash of file path as key if the value is null or empty].</param>
        /// <returns>The key for cache file.</returns>
        public string CopyFrom(string file, string key = null)
        {
            if (!File.Exists(file))
            {
                return(null);
            }

            if (string.IsNullOrEmpty(key))
            {
                key = MD5CryptoUtility.ComputeHash(file);
            }

            var cacheFile = ResolveFile(key);

            DirectoryUtility.RequireDirectory(cacheFile);
            File.Copy(file, cacheFile, true);
            return(key);
        }
示例#6
0
        /// <summary>
        /// Find the cache file from cache by key.
        /// </summary>
        /// <param name="key">Key for cache file.</param>
        /// <param name="md5Hash">Matching file md5 hash [Do not check md5 hash if the value is null or empty].</param>
        /// <returns>The path of cache file from cache by key.</returns>
        public string FindFile(string key, string md5Hash)
        {
            var filePath = ResolveFile(key);

            if (File.Exists(filePath))
            {
                if (string.IsNullOrEmpty(md5Hash))
                {
                    return(filePath);
                }

                var fileMd5 = MD5CryptoUtility.ComputeFileHash(filePath);
                if (fileMd5.Equals(md5Hash))
                {
                    return(filePath);
                }
            }
            return(null);
        }