Exemplo n.º 1
0
        /// <summary>
        /// Gets the currently selected hash algorithm for the HASH command.
        /// </summary>
        internal FtpHashAlgorithm GetHashAlgorithmUnused()
        {
            FtpReply reply;
            var      type = FtpHashAlgorithm.NONE;

#if !CORE14
            lock (m_lock) {
#endif
            LogFunc(nameof(GetHashAlgorithmUnused));

            if ((reply = Execute("OPTS HASH")).Success)
            {
                try {
                    type = HashAlgos.FromString(reply.Message);
                }
                catch (InvalidOperationException ex) {
                    // Do nothing
                }
            }

#if !CORE14
        }
#endif

            return(type);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the currently selected hash algorithm for the HASH command asynchronously.
        /// </summary>
        internal async Task <FtpHashAlgorithm> GetHashAlgorithmUnusedAsync(CancellationToken token = default(CancellationToken))
        {
            FtpReply reply;
            var      type = FtpHashAlgorithm.NONE;

            LogFunc(nameof(GetHashAlgorithmUnusedAsync));

            if ((reply = await ExecuteAsync("OPTS HASH", token)).Success)
            {
                try {
                    type = HashAlgos.FromString(reply.Message);
                }
                catch (InvalidOperationException ex) {
                    // Do nothing
                }
            }

            return(type);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses the recieved FTP hash response into a new FtpHash object.
        /// </summary>
        public static FtpHash Parse(string reply)
        {
            // Current draft says the server should return this:
            //		SHA-256 0-49 169cd22282da7f147cb491e559e9dd filename.ext

            // Current version of FileZilla returns this:
            //		SHA-1 21c2ca15cf570582949eb59fb78038b9c27ffcaf

            // Real reply that was failing:
            //		213 MD5 0-170500096 3197bf4ec5fa2d441c0f50264ca52f11


            var hash = new FtpHash();

            // FIX #722 - remove the FTP status code causing a wrong hash to be returned
            if (reply.StartsWith("2") && reply.Length > 10)
            {
                reply = reply.Substring(4);
            }

            Match m;

            if (!(m = Regex.Match(reply,
                                  @"(?<algorithm>.+)\s" +
                                  @"(?<bytestart>\d+)-(?<byteend>\d+)\s" +
                                  @"(?<hash>.+)\s" +
                                  @"(?<filename>.+)")).Success)
            {
                m = Regex.Match(reply, @"(?<algorithm>.+)\s(?<hash>.+)\s");
            }

            if (m != null && m.Success)
            {
                hash.Algorithm = HashAlgorithms.FromString(m.Groups["algorithm"].Value);
                hash.Value     = m.Groups["hash"].Value;
            }
            else
            {
                // failed to parse
            }

            return(hash);
        }