Ejemplo n.º 1
0
        /// <summary>
        /// Verifies the file.
        /// </summary>
        /// <param name="baseDirectory">The base directory.</param>
        /// <param name="verifyData">The verify data.</param>
        /// <param name="verificationMode">The verification mode.</param>
        /// <returns></returns>
        public static bool VerifyFile(string baseDirectory, IVerifiableFile verifyData, VerifyMode verificationMode)
        {
            if (string.IsNullOrEmpty(baseDirectory))
            {
                throw new ArgumentNullException("baseDirectory");
            }
            else if (verifyData == null)
            {
                throw new ArgumentNullException("verifyData");
            }

            string path = QQnPath.Combine(baseDirectory, verifyData.Filename);

            FileInfo fif = new FileInfo(path);

            if (!fif.Exists)
            {
                return(false);
            }

            if (verificationMode < VerifyMode.Time)
            {
                return(true);
            }

            bool hasTime    = verifyData.LastWriteTimeUtc.HasValue;
            bool timeFailed = hasTime && fif.LastWriteTimeUtc != verifyData.LastWriteTimeUtc.Value;

            if (timeFailed && verificationMode == VerifyMode.Time)
            {
                return(false);
            }

            if (verificationMode < VerifyMode.TimeSize)
            {
                return(true);
            }

            bool hasSize    = (verifyData.FileSize >= 0);
            bool sizeFailed = hasSize && fif.Length != verifyData.FileSize;

            if ((timeFailed || sizeFailed) && verificationMode == VerifyMode.TimeSize)
            {
                return(false);
            }

            if (verificationMode < VerifyMode.FileHash)
            {
                return(true);
            }

            if (!string.IsNullOrEmpty(verifyData.FileHash) && !QQnCryptoHelpers.VerifyFileHash(fif.FullName, verifyData.FileHash))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks whether the two verifiers verify the same file
        /// </summary>
        /// <param name="one">The one.</param>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public static bool AreEqualVerifiers(IVerifiableFile one, IVerifiableFile other)
        {
            if (one == null)
            {
                throw new ArgumentNullException("one");
            }
            else if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            if ((one.FileHash != null) && (other.FileHash != null))
            {
                // Compare hashes without size
                if (!string.Equals(QQnCryptoHelpers.NormalizeHashValue(one.FileHash, true), QQnCryptoHelpers.NormalizeHashValue(other.FileHash, true)))
                {
                    return(false);
                }
            }

            long oneSize   = one.FileSize;
            long otherSize = other.FileSize;

            // Get sizes from hash if they are not available directly
            if ((oneSize < 0) && one.FileHash != null)
            {
                oneSize = QQnCryptoHelpers.GetSizeFromHash(one.FileHash);
            }

            if ((otherSize < 0) && other.FileHash != null)
            {
                otherSize = QQnCryptoHelpers.GetSizeFromHash(other.FileHash);
            }

            if ((oneSize >= 0) && (otherSize >= 0) && (oneSize != otherSize))
            {
                return(false);
            }

            if (one.LastWriteTimeUtc.HasValue && other.LastWriteTimeUtc.HasValue && one.LastWriteTimeUtc.Value != other.LastWriteTimeUtc.Value)
            {
                return(false);
            }

            return(true);
        }