Mono.Security.StrongName.Sign C# (CSharp) Méthode

Sign() public méthode

public Sign ( string fileName ) : bool
fileName string
Résultat bool
		public bool Sign (string fileName) 
		{
			bool result = false;
			StrongNameSignature sn;
			using (FileStream fs = File.OpenRead (fileName)) {
				sn = StrongHash (fs, StrongNameOptions.Signature);
				fs.Close ();
			}
			if (sn.Hash == null)
				return false;

			byte[] signature = null;
			try {
				RSAPKCS1SignatureFormatter sign = new RSAPKCS1SignatureFormatter (rsa);
				sign.SetHashAlgorithm (TokenAlgorithm);
				signature = sign.CreateSignature (sn.Hash);
				Array.Reverse (signature);
			}
			catch (CryptographicException) {
				return false;
			}

			using (FileStream fs = File.OpenWrite (fileName)) {
				fs.Position = sn.SignaturePosition;
				fs.Write (signature, 0, signature.Length);
				fs.Close ();
				result = true;
			}
			return result;
		}

Usage Example

Exemple #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;
        }
All Usage Examples Of Mono.Security.StrongName::Sign