예제 #1
0
        /// <summary>
        /// Sets the hash algorithm on the server to be used with the HASH command asynchronously.
        /// </summary>
        internal async Task SetHashAlgorithmInternalAsync(FtpHashAlgorithm algorithm, CancellationToken token = default(CancellationToken))
        {
            FtpReply reply;

            // skip setting the hash algo if the server is already configured to it
            if (_LastHashAlgo == algorithm)
            {
                return;
            }

            if ((HashAlgorithms & algorithm) != algorithm)
            {
                throw new NotImplementedException("The hash algorithm " + algorithm.ToString() + " was not advertised by the server.");
            }

            string algoName = HashAlgos.PrintToString(algorithm);

            if (!(reply = await ExecuteAsync("OPTS HASH " + algoName, token)).Success)
            {
                throw new FtpCommandException(reply);
            }

            // save the current hash algo so no need to repeat this command
            _LastHashAlgo = algorithm;
        }
예제 #2
0
        /// <summary>
        /// Sets the hash algorithm on the server to use for the HASH command.
        /// </summary>
        internal void SetHashAlgorithmInternal(FtpHashAlgorithm algorithm)
        {
            FtpReply reply;

            // skip setting the hash algo if the server is already configured to it
            if (_LastHashAlgo == algorithm)
            {
                return;
            }

#if !CORE14
            lock (m_lock) {
#endif
            if ((HashAlgorithms & algorithm) != algorithm)
            {
                throw new NotImplementedException("The hash algorithm " + algorithm.ToString() + " was not advertised by the server.");
            }

            string algoName = HashAlgos.PrintToString(algorithm);

            if (!(reply = Execute("OPTS HASH " + algoName)).Success)
            {
                throw new FtpCommandException(reply);
            }

            // save the current hash algo so no need to repeat this command
            _LastHashAlgo = algorithm;

#if !CORE14
        }
#endif
        }
예제 #3
0
파일: FtpHash.cs 프로젝트: zkrat/FluentFTP
        /// <summary>
        /// Computes the hash for the specified stream and compares
        /// it to the value in this object. CRC hashes are not supported
        /// because there is no built-in support in the .net framework and
        /// a CRC implementation exceeds the scope of this project. If you
        /// attempt to Verify() a CRC hash a NotImplemented() exception will
        /// be thrown.
        /// </summary>
        /// <param name="istream">The stream to compute the hash for</param>
        /// <returns>True if the computed hash matches what's stored in this object.</returns>
        public bool Verify(Stream istream)
        {
            if (IsValid)
            {
                HashAlgorithm hashAlg = null;

                switch (m_algorithm)
                {
                case FtpHashAlgorithm.SHA1:
                    hashAlg = new SHA1CryptoServiceProvider();
                    break;

#if !NET2
                case FtpHashAlgorithm.SHA256:
                    hashAlg = new SHA256CryptoServiceProvider();
                    break;

                case FtpHashAlgorithm.SHA512:
                    hashAlg = new SHA512CryptoServiceProvider();
                    break;
#endif
                case FtpHashAlgorithm.MD5:
                    hashAlg = new MD5CryptoServiceProvider();
                    break;

                case FtpHashAlgorithm.CRC:
                    throw new NotImplementedException("There is no built in support for computing CRC hashes.");

                default:
                    throw new NotImplementedException("Unknown hash algorithm: " + m_algorithm.ToString());
                }

                try {
                    byte[] data = null;
                    string hash = "";

                    data = hashAlg.ComputeHash(istream);
                    if (data != null)
                    {
                        foreach (byte b in data)
                        {
                            hash += b.ToString("x2");
                        }

                        return(hash.ToUpper() == m_value.ToUpper());
                    }
                }
                finally {
#if !NET2 // .NET 2.0 doesn't provide access to Dispose() for HashAlgorithm
                    if (hashAlg != null)
                    {
                        hashAlg.Dispose();
                    }
#endif
                }
            }

            return(false);
        }
예제 #4
0
        /// <summary>
        /// Sets the hash algorithm on the server to use for the HASH command.
        /// </summary>
        /// <remarks>
        /// If you specify an algorithm not listed in <see cref="FtpClient.HashAlgorithms"/>
        /// a <see cref="NotImplementedException"/> will be thrown
        /// so be sure to query that list of Flags before
        /// selecting a hash algorithm. Support for the
        /// HASH command is experimental. Please see
        /// the following link for more details:
        /// http://tools.ietf.org/html/draft-bryan-ftpext-hash-02
        /// </remarks>
        /// <param name="type">Hash Algorithm</param>
        /// <exception cref="System.NotImplementedException">Thrown if the selected algorithm is not available on the server</exception>
        /// <example><code source="..\Examples\SetHashAlgorithm.cs" lang="cs" /></example>
        public void SetHashAlgorithm(FtpHashAlgorithm type)
        {
            FtpReply reply;
            string   algorithm;

#if !CORE14
            lock (m_lock) {
#endif
            if ((HashAlgorithms & type) != type)
            {
                throw new NotImplementedException("The hash algorithm " + type.ToString() + " was not advertised by the server.");
            }

            switch (type)
            {
            case FtpHashAlgorithm.SHA1:
                algorithm = "SHA-1";
                break;

            case FtpHashAlgorithm.SHA256:
                algorithm = "SHA-256";
                break;

            case FtpHashAlgorithm.SHA512:
                algorithm = "SHA-512";
                break;

            case FtpHashAlgorithm.MD5:
                algorithm = "MD5";
                break;

            default:
                algorithm = type.ToString();
                break;
            }

            if (!(reply = Execute("OPTS HASH " + algorithm)).Success)
            {
                throw new FtpCommandException(reply);
            }

#if !CORE14
        }
#endif
        }
예제 #5
0
        /// <summary>
        /// Get string representation of FtpHashAlgorithm
        /// </summary>
        /// <param name="ftpHashAlgorithm">FtpHashAlgorithm to be converted into string</param>
        /// <returns>Name of the hash algorithm</returns>
        public static string ToString(FtpHashAlgorithm ftpHashAlgorithm)
        {
            if (!EnumToName.ContainsKey(ftpHashAlgorithm))
            {
                return(ftpHashAlgorithm.ToString());
            }

            return(EnumToName[ftpHashAlgorithm]);
        }
예제 #6
0
        /// <summary>
        /// Get string representation of FtpHashAlgorithm
        /// </summary>
        /// <param name="name">FtpHashAlgorithm to be converted into string</param>
        /// <returns>Name of the hash algorithm</returns>
        public static string PrintToString(this FtpHashAlgorithm name)
        {
            if (!EnumToName.ContainsKey(name))
            {
                return(name.ToString());
            }

            return(EnumToName[name]);
        }
예제 #7
0
        /// <summary>
        /// Sets the hash algorithm on the server to be used with the HASH command asynchronously.
        /// </summary>
        /// <param name="type">Hash algorithm to use</param>
        /// <param name="token">The token that can be used to cancel the entire process</param>
        /// <exception cref="System.NotImplementedException">Thrown if the selected algorithm is not available on the server</exception>
        public async Task SetHashAlgorithmAsync(FtpHashAlgorithm type, CancellationToken token = default(CancellationToken))
        {
            FtpReply reply;
            string   algorithm;

            if ((HashAlgorithms & type) != type)
            {
                throw new NotImplementedException("The hash algorithm " + type.ToString() + " was not advertised by the server.");
            }

            algorithm = FtpHashAlgorithms.ToString(type);

            if (!(reply = await ExecuteAsync("OPTS HASH " + algorithm, token)).Success)
            {
                throw new FtpCommandException(reply);
            }
        }
예제 #8
0
        /// <summary>
        /// Sets the hash algorithm on the server to use for the HASH command.
        /// </summary>
        /// <remarks>
        /// If you specify an algorithm not listed in <see cref="FtpClient.HashAlgorithms"/>
        /// a <see cref="NotImplementedException"/> will be thrown
        /// so be sure to query that list of Flags before
        /// selecting a hash algorithm. Support for the
        /// HASH command is experimental. Please see
        /// the following link for more details:
        /// http://tools.ietf.org/html/draft-bryan-ftpext-hash-02
        /// </remarks>
        /// <param name="type">Hash Algorithm</param>
        /// <exception cref="System.NotImplementedException">Thrown if the selected algorithm is not available on the server</exception>
        /// <example><code source="..\Examples\SetHashAlgorithm.cs" lang="cs" /></example>
        public void SetHashAlgorithm(FtpHashAlgorithm type)
        {
            FtpReply reply;
            string   algorithm;

#if !CORE14
            lock (m_lock) {
#endif
            if ((HashAlgorithms & type) != type)
            {
                throw new NotImplementedException("The hash algorithm " + type.ToString() + " was not advertised by the server.");
            }

            algorithm = FtpHashAlgorithms.ToString(type);

            if (!(reply = Execute("OPTS HASH " + algorithm)).Success)
            {
                throw new FtpCommandException(reply);
            }

#if !CORE14
        }
#endif
        }