Sign() public method

public Sign ( string fileName ) : bool
fileName string
return bool
Beispiel #1
0
        void Save(string assemblyFileName, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine)
        {
            this.peKind  = portableExecutableKind;
            this.machine = imageFileMachine;

            if ((peKind & PortableExecutableKinds.PE32Plus) != 0 || (peKind & PortableExecutableKinds.Unmanaged32Bit) != 0)
            {
                throw new NotImplementedException(peKind.ToString());
            }
            if (machine == ImageFileMachine.IA64 || machine == ImageFileMachine.AMD64)
            {
                throw new NotImplementedException(machine.ToString());
            }

            if (resource_writers != null)
            {
                foreach (IResourceWriter writer in resource_writers)
                {
                    writer.Generate();
                    writer.Close();
                }
            }

            // Create a main module if not already created
            ModuleBuilder mainModule = null;

            if (modules != null)
            {
                foreach (ModuleBuilder module in modules)
                {
                    if (module.FullyQualifiedName == assemblyFileName)
                    {
                        mainModule = module;
                    }
                }
            }
            if (mainModule == null)
            {
                mainModule = DefineDynamicModule("RefEmit_OnDiskManifestModule", assemblyFileName);
            }

            if (!is_module_only)
            {
                mainModule.IsMain = true;
            }

            /*
             * Create a new entry point if the one specified
             * by the user is in another module.
             */
            if ((entry_point != null) && entry_point.DeclaringType.Module != mainModule)
            {
                Type[] paramTypes;
                if (entry_point.GetParametersCount() == 1)
                {
                    paramTypes = new Type [] { typeof(string) }
                }
                ;
                else
                {
                    paramTypes = Type.EmptyTypes;
                }

                MethodBuilder mb    = mainModule.DefineGlobalMethod("__EntryPoint$", MethodAttributes.Static | MethodAttributes.PrivateScope, entry_point.ReturnType, paramTypes);
                ILGenerator   ilgen = mb.GetILGenerator();
                if (paramTypes.Length == 1)
                {
                    ilgen.Emit(OpCodes.Ldarg_0);
                }
                ilgen.Emit(OpCodes.Tailcall);
                ilgen.Emit(OpCodes.Call, entry_point);
                ilgen.Emit(OpCodes.Ret);

                entry_point = mb;
            }

            if (version_res != null)
            {
                DefineVersionInfoResourceImpl(assemblyFileName);
            }

            if (sn != null)
            {
                // runtime needs to value to embed it into the assembly
                public_key = sn.PublicKey;
            }

            foreach (ModuleBuilder module in modules)
            {
                if (module != mainModule)
                {
                    module.Save();
                }
            }

            // Write out the main module at the end, because it needs to
            // contain the hash of the other modules
            mainModule.Save();

            if ((sn != null) && (sn.CanSign))
            {
                sn.Sign(System.IO.Path.Combine(this.AssemblyDir, assemblyFileName));
            }

            created = true;
        }
Beispiel #2
0
Datei: sn.cs Projekt: nobled/mono
		static bool ReSign (string assemblyName, RSA key, bool quiet) 
		{
			// this doesn't load the assembly (well it unloads it ;)
			// http://weblogs.asp.net/nunitaddin/posts/9991.aspx
			AssemblyName an = null;
			try {
				an = AssemblyName.GetAssemblyName (assemblyName);
			}
			catch {
			}
			if (an == null) {
				Console.WriteLine ("Unable to load assembly: {0}", assemblyName);
				return false;
			}

			StrongName sign = new StrongName (key);
			byte[] token = an.GetPublicKeyToken ();

			// first, try to compare using a mapped public key (e.g. ECMA)
			bool same = Compare (sign.PublicKey, StrongNameManager.GetMappedPublicKey (token));
			if (!same) {
				// second, try to compare using the assembly public key
				same = Compare (sign.PublicKey, an.GetPublicKey ());
				if (!same) {
					// third (and last) chance, try to compare public key token
					same = Compare (sign.PublicKeyToken, token);
				}
			}

			if (same) {
				bool signed = sign.Sign (assemblyName);
				if (!quiet || !signed) {
					Console.WriteLine (signed ? "Assembly {0} signed." : "Couldn't sign the assembly {0}.", 
							   assemblyName);
				}
				return signed;
			}
			
			Console.WriteLine ("Couldn't sign the assembly {0} with this key pair.", assemblyName);
			return false;
		}