public static HashValue?GetHashFromCache(IFile file, string algorithmName)
        {
            if (algorithmName == null)
            {
                return(null);
            }

            if (StandardFileHashingService.ConfigurationSection.Default.CanCacheHash(file))
            {
                string s;
                var    bytes = (byte[])file.Attributes["extended:" + algorithmName];

                if (bytes == null)
                {
                    return(null);
                }

                if ((s = Encoding.ASCII.GetString(bytes)) != "")
                {
                    return(new HashValue(TextConversion.FromHexString(s), algorithmName, 0, -1));
                }
            }

            return(null);
        }
Esempio n. 2
0
        public override HashValue ComputeHash(long offset, long length)
        {
            MeteringStream stream;

            if (offset == 0 && length == -1 && !this.OperatingStream.CanSeek)
            {
                var retval = StandardFileHashingService.GetHashFromCache(this.OperatingNode, this.ServiceType.AlgorithmName);

                if (retval != null)
                {
                    return(retval.Value);
                }

                stream = new MeteringStream(this.OperatingStream);

                return(new HashValue(this.hashAlgorithm.ComputeHash(stream), this.ServiceType.AlgorithmName, 0, Convert.ToInt32(stream.ReadMeter.Value)));
            }
            else
            {
                if (offset == 0 && length == this.OperatingNode.Length)
                {
                    if (StandardFileHashingService.ConfigurationSection.Default.CanCacheHash(this.OperatingNode))
                    {
                        string s;

                        if ((s = Convert.ToString(this.OperatingNode.Attributes["extended:" + ServiceType.AlgorithmName])) != "")
                        {
                            return(new HashValue(TextConversion.FromHexString(s), this.ServiceType.AlgorithmName, 0, -1));
                        }
                    }
                }

                if (!this.OperatingStream.CanSeek)
                {
                    throw new NotSupportedException("ComputeHash_StreamCannotSeek");
                }

                stream = new MeteringStream(new PartialStream(this.OperatingStream, offset, length));

                return(new HashValue(this.hashAlgorithm.ComputeHash(stream), this.ServiceType.AlgorithmName, offset, Convert.ToInt64(stream.ReadMeter.Value)));
            }
        }
        public override HashValue ComputeHash(string uri, NodeType nodeType, string algorithm, bool recursive, long offset, long length, IEnumerable <string> fileAttributes, IEnumerable <string> dirAttributes)
        {
            StringBuilder dirAttributesString  = null;
            StringBuilder fileAttributesString = null;

            using (this.AcquireCommandContext())
            {
                try
                {
                    if (dirAttributes != null)
                    {
                        dirAttributesString = new StringBuilder();

                        foreach (string s in dirAttributes)
                        {
                            dirAttributesString.Append(s);
                            dirAttributesString.Append(',');
                        }

                        if (dirAttributesString.Length > 0)
                        {
                            dirAttributesString.Length--;
                        }
                        else
                        {
                            dirAttributesString = null;
                        }
                    }

                    if (fileAttributes != null)
                    {
                        fileAttributesString = new StringBuilder();

                        foreach (string s in fileAttributes)
                        {
                            fileAttributesString.Append(s);
                            fileAttributesString.Append(',');
                        }

                        if (fileAttributesString.Length > 0)
                        {
                            fileAttributesString.Length--;
                        }
                        else
                        {
                            fileAttributesString = null;
                        }
                    }

                    StringBuilder commandText = new StringBuilder(128);

                    commandText.Append("computehash -hex");
                    commandText.Append(" -t=\"").Append(TextNetworkProtocol.GetNodeTypeName(nodeType)).Append('\"');

                    if (offset != 0)
                    {
                        commandText.Append(" -o=\"").Append(offset).Append('\"');
                    }

                    if (recursive)
                    {
                        commandText.Append(" -r");
                    }

                    if (length != -1)
                    {
                        commandText.Append(" -l=\"").Append(length).Append('\"');
                    }

                    if (algorithm != "md5")
                    {
                        commandText.Append(" -a=\"").Append(algorithm).Append('\"');
                    }

                    if (fileAttributesString != null)
                    {
                        commandText.Append(" -fileattribs=\"").Append(fileAttributesString).Append('\"');
                    }

                    if (dirAttributesString != null)
                    {
                        commandText.Append(" -dirattribs=\"").Append(dirAttributesString).Append('\"');
                    }

                    commandText.Append(" \"").Append(uri).Append('\"');

                    var response = SendCommand(DefaultTryCount, commandText.ToString()).ProcessError();

                    return(new HashValue
                           (
                               TextConversion.FromHexString(response.ResponseTuples["hash"]),
                               algorithm,
                               0,
                               length
                           ));
                }
                catch (TextNetworkProtocolException)
                {
                    this.connected = false;

                    throw;
                }
                catch (IOException)
                {
                    this.connected = false;

                    throw;
                }
            }
        }