示例#1
0
        public string hash_file(string filePath)
        {
            if (!_fileSystem.file_exists(filePath))
            {
                return(string.Empty);
            }

            try
            {
                var hash = _hashAlgorithm.ComputeHash(_fileSystem.read_file_bytes(filePath));

                return(BitConverter.ToString(hash).Replace("-", string.Empty));
            }
            catch (IOException ex)
            {
                this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file or file too big instead.{1} Captured error:{1}  {2}".format_with(filePath, Environment.NewLine, ex.Message));

                if (file_is_locked(ex))
                {
                    return(ApplicationParameters.HashProviderFileLocked);
                }

                //IO.IO_FileTooLong2GB (over Int32.MaxValue)
                return(ApplicationParameters.HashProviderFileTooBig);
            }
        }
示例#2
0
        /// <summary>
        /// The calculate hash from string.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <param name="algorithm">
        /// The algorithm.
        /// </param>
        /// <returns>
        /// The <see cref="byte[]"/>.
        /// </returns>
        public static byte[] CalculateHashFromString(string value, IHashAlgorithm algorithm)
        {
            var    buf    = Encoding.GetEncoding("Windows-1251").GetBytes(value);
            Stream stream = new MemoryStream(buf);

            return(algorithm.ComputeHash(stream));
        }
        public string GenerateId(IHashAlgorithm hashAlgorithm)
        {
            Debug.Assert(hashAlgorithm != null);

            this.Id = hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(this.Process + this.Title)).ToHex();

            return(this.Id);
        }
 private int ComputeHash(int size, string word, int hashId)
 {
     unchecked
     {
         var bytes = word.SelectMany(c => Encoding.UTF8.GetBytes(new[] { char.ToLowerInvariant(c) }).Concat(BitConverter.GetBytes(hashId)));
         var hash = _hashAlgorithm.ComputeHash(bytes);
         return (int)(hash % size);
     }
 }
示例#5
0
        public static string hash_value(string originalText, CryptoHashProviderType providerType)
        {
            IHashAlgorithm hashAlgorithm = get_hash_algorithm_static(providerType);

            if (hashAlgorithm == null)
            {
                return(string.Empty);
            }

            var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(originalText));

            return(BitConverter.ToString(hash).Replace("-", string.Empty));
        }
示例#6
0
        public static string Hash(string text, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(text);
            }

            IHashAlgorithm hashAlgorithm = null;

            try
            {
                hashAlgorithm = CreateHashAlgorithm(encoding);
                return(hashAlgorithm.ComputeHash(text));
            }
            finally
            {
                ObjectHelper.Dispose(ref hashAlgorithm);
            }
        }
示例#7
0
        public string hash_file(string filePath)
        {
            if (!_fileSystem.file_exists(filePath))
            {
                return(string.Empty);
            }

            try
            {
                var hash = _hashAlgorithm.ComputeHash(_fileSystem.read_file_bytes(filePath));

                return(BitConverter.ToString(hash).Replace("-", string.Empty));
            }
            catch (IOException ex)
            {
                this.Log().Warn(() => "Error computing hash for '{0}'{1} Captured error:{1}  {2}".format_with(filePath, Environment.NewLine, ex.Message));
                //IO.IO_FileTooLong2GB (over Int32.MaxValue)
                return(ApplicationParameters.HashProviderFileTooBig);

                return("UnableToDetectChanges_FileTooBig");
            }
        }
示例#8
0
        public static string ComputeHex(this IHashAlgorithm hashAlgorithm, Stream stream)
        {
            var hash = hashAlgorithm.ComputeHash(stream);

            return(ComputeHex(hash));
        }
示例#9
0
 public static string ComputeHashBase64String(this IHashAlgorithm hashAlgorithm, string value)
 {
     return(Convert.ToBase64String(hashAlgorithm.ComputeHash(value)));
 }
示例#10
0
 public virtual byte[] ComputeHash(byte[] buffer, int offset, int count)
 {
     return(_hashAlgorithm.ComputeHash(buffer, offset, count));
 }
示例#11
0
        public static string ComputeHexWithEncoding(this IHashAlgorithm hashAlgorithm, string value)
        {
            var hash = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(value));

            return(hash.ComputeHex());
        }
示例#12
0
 public static string ComputeHashString(this IHashAlgorithm hashAlgorithm, string value)
 {
     return(Encoding.Default.GetString(hashAlgorithm.ComputeHash(value)));
 }
示例#13
0
        public static string ComputeHex(this IHashAlgorithm hashAlgorithm, string value)
        {
            var hash = hashAlgorithm.ComputeHash(value);

            return(hash.ComputeHex());
        }
示例#14
0
        public void BuildDelta(IHashAlgorithm hashAlgorithm, Stream newFileStream, ISignatureReader signatureReader, IDeltaWriter deltaWriter)
        {
            var signature = signatureReader.ReadSignature();

            var hash = hashAlgorithm.ComputeHash(newFileStream);

            newFileStream.Seek(0, SeekOrigin.Begin);

            deltaWriter.WriteMetadata(hashAlgorithm, hash, newFileStream.Length, signature.ChunkSize);

            var fileSize = newFileStream.Length;

            ProgressReporter.ReportProgress("Building delta", 0, fileSize);

            var chunkSize = signature.ChunkSize;

            var        missingPosition   = newFileStream.Position;
            RingBuffer buffer            = new RingBuffer(newFileStream, chunkSize);
            var        checksumAlgorithm = signature.RollingChecksumAlgorithm;
            uint       checksum          = checksumAlgorithm.Calculate(buffer.GetBuffer(), 0, chunkSize);

            while (buffer.IsEnd == false)
            {
                ProgressReporter.ReportProgress("Building delta", newFileStream.Position, fileSize);

                var list = signature.FindChecksum(checksum);

                if (list != null)
                {
                    var sha       = signature.HashAlgorithm.ComputeHash(buffer.GetBuffer(), 0, chunkSize);
                    var chunkHash = list.Find(sha);
                    if (chunkHash != null)
                    {
                        long offset = chunkHash.FindBestOffset(deltaWriter.LastOffset);

                        if (missingPosition < newFileStream.Position - chunkSize)
                        {
                            deltaWriter.WriteDataCommand(newFileStream, missingPosition, newFileStream.Position - chunkSize - missingPosition);
                        }

                        deltaWriter.WriteCopyCommand(new DataRange(offset, chunkSize));
                        missingPosition = newFileStream.Position;

                        buffer.ReadNew();
                        checksum = checksumAlgorithm.Calculate(buffer.GetBuffer(), 0, chunkSize);

                        continue;
                    }
                }

                var remove = buffer.Peek();
                var add    = buffer.ReadContinue();
                checksum = checksumAlgorithm.Rotate(checksum, remove, add, chunkSize);
            }

            if (newFileStream.Length != missingPosition)
            {
                deltaWriter.WriteDataCommand(newFileStream, missingPosition, newFileStream.Length - missingPosition);
            }
            ProgressReporter.ReportProgress("Building delta", newFileStream.Position, fileSize);

            deltaWriter.Finish();
        }
示例#15
0
        public static byte[] ComputeHash(this IHashAlgorithm hashAlgorithm, string value)
        {
            var hash = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(value));

            return(hash);
        }
示例#16
0
        public static string ComputeHex(this IHashAlgorithm hashAlgorithm, byte[] buffer)
        {
            var hash = hashAlgorithm.ComputeHash(buffer);

            return(hash.ComputeHex());
        }
示例#17
0
        public static string ComputeHex(this IHashAlgorithm hashAlgorithm, byte[] buffer, int offset, int count)
        {
            var hash = hashAlgorithm.ComputeHash(buffer, offset, count);

            return(hash.ComputeHex());
        }