Esempio n. 1
0
        ///////////////////////////////////////////////////////////////////////

        public bool VerifyFile(
            Configuration configuration,
            string fileName,
            bool strongName
            )
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    Trace(configuration, String.Format(
                              "File \"{0}\" does not exist.", fileName),
                          TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                string error = null;

                if (strongName)
                {
#if NATIVE && WINDOWS
                    if (VersionOps.IsWindowsOperatingSystem() &&
                        !StrongNameEx.IsStrongNameSigned(
                            configuration, fileName, true, ref error))
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" is not signed.",
                                  fileName), TraceCategory);

                        Trace(configuration, String.Format(
                                  "Assembly signature error: {0}", error),
                              TraceCategory);

                        return(false);
                    }
#endif

                    ///////////////////////////////////////////////////////////

                    AssemblyName assemblyName =
                        AssemblyName.GetAssemblyName(fileName);

                    if (assemblyName == null)
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" has no name.", fileName),
                              TraceCategory);

                        return(false);
                    }

                    byte[] filePublicKeyToken = assemblyName.GetPublicKeyToken();

                    if (!GenericOps <byte> .Equals(
                            filePublicKeyToken, publicKeyToken))
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" has incorrect " +
                                  "public key token \"{1}\".", fileName,
                                  FormatOps.ToHexString(filePublicKeyToken)),
                              TraceCategory);

                        return(false);
                    }
                }

                ///////////////////////////////////////////////////////////////

                byte[] hash = null;

                if (FileOps.Hash(
                        configuration, "md5", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, md5Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" MD5 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                if (FileOps.Hash(
                        configuration, "sha1", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, sha1Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" SHA1 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                if (FileOps.Hash(
                        configuration, "sha512", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, sha512Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" SHA512 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                return(true);
            }
            catch (Exception e)
            {
                Trace(configuration, e, TraceCategory);
            }

            return(false);
        }