Exemplo n.º 1
0
 private static byte[] CreateStrongName(StrongNameKeyPair key_pair, byte[] hash)
 {
     using (RSA key = key_pair.CreateRSA())
     {
         RSAPKCS1SignatureFormatter rSAPKCS1SignatureFormatter = new RSAPKCS1SignatureFormatter(key);
         rSAPKCS1SignatureFormatter.SetHashAlgorithm("SHA1");
         byte[] array = rSAPKCS1SignatureFormatter.CreateSignature(hash);
         Array.Reverse(array);
         return(array);
     }
 }
Exemplo n.º 2
0
        private static void StrongName(Stream stream, StrongNameKeyPair keyPair, uint headerLength, uint textSectionFileOffset, uint strongNameSignatureFileOffset, uint strongNameSignatureLength)
        {
            SHA1Managed hash = new SHA1Managed();

            using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
            {
                stream.Seek(0, SeekOrigin.Begin);
                byte[] buf = new byte[8192];
                HashChunk(stream, cs, buf, (int)headerLength);
                stream.Seek(textSectionFileOffset, SeekOrigin.Begin);
                HashChunk(stream, cs, buf, (int)(strongNameSignatureFileOffset - textSectionFileOffset));
                stream.Seek(strongNameSignatureLength, SeekOrigin.Current);
                HashChunk(stream, cs, buf, (int)(stream.Length - (strongNameSignatureFileOffset + strongNameSignatureLength)));
            }
            using (RSA rsa = keyPair.CreateRSA())
            {
                RSAPKCS1SignatureFormatter sign = new RSAPKCS1SignatureFormatter(rsa);
                byte[] signature = sign.CreateSignature(hash);
                Array.Reverse(signature);
                if (signature.Length != strongNameSignatureLength)
                {
                    throw new InvalidOperationException("Signature length mismatch");
                }
                stream.Seek(strongNameSignatureFileOffset, SeekOrigin.Begin);
                stream.Write(signature, 0, signature.Length);
            }

            // compute the PE checksum
            stream.Seek(0, SeekOrigin.Begin);
            int          count = (int)stream.Length / 4;
            BinaryReader br    = new BinaryReader(stream);
            long         sum   = 0;

            for (int i = 0; i < count; i++)
            {
                sum += br.ReadUInt32();
                int carry = (int)(sum >> 32);
                sum &= 0xFFFFFFFFU;
                sum += carry;
            }
            while ((sum >> 16) != 0)
            {
                sum = (sum & 0xFFFF) + (sum >> 16);
            }
            sum += stream.Length;

            // write the PE checksum, note that it is always at offset 0xD8 in the file
            ByteBuffer bb = new ByteBuffer(4);

            bb.Write((int)sum);
            stream.Seek(0xD8, SeekOrigin.Begin);
            bb.WriteTo(stream);
        }
Exemplo n.º 3
0
 private static byte[] CreateStrongName(StrongNameKeyPair key_pair, byte[] hash)
 {
     byte[] numArray;
     using (RSA rSA = key_pair.CreateRSA())
     {
         RSAPKCS1SignatureFormatter rSAPKCS1SignatureFormatter = new RSAPKCS1SignatureFormatter(rSA);
         rSAPKCS1SignatureFormatter.SetHashAlgorithm("SHA1");
         byte[] numArray1 = rSAPKCS1SignatureFormatter.CreateSignature(hash);
         Array.Reverse((Array)numArray1);
         numArray = numArray1;
     }
     return(numArray);
 }
Exemplo n.º 4
0
        static byte [] CreateStrongName(StrongNameKeyPair key_pair, byte [] hash)
        {
            const string hash_algo = "SHA1";

            using (var rsa = key_pair.CreateRSA()) {
                var formatter = new RSAPKCS1SignatureFormatter(rsa);
                formatter.SetHashAlgorithm(hash_algo);

                byte [] signature = formatter.CreateSignature(hash);
                Array.Reverse(signature);

                return(signature);
            }
        }
Exemplo n.º 5
0
        private static void StrongName(Stream stream, StrongNameKeyPair keyPair, uint headerLength, uint textSectionFileOffset, uint strongNameSignatureFileOffset, uint strongNameSignatureLength)
        {
            byte[] hash;
            using (SHA1 sha1 = SHA1.Create())
            {
                stream.Seek(0, SeekOrigin.Begin);
                Stream skipStream = new SkipStream(stream, strongNameSignatureFileOffset, strongNameSignatureLength);
                skipStream = new SkipStream(skipStream, headerLength, textSectionFileOffset - headerLength);
                hash       = sha1.ComputeHash(skipStream);
            }
            using (RSACryptoServiceProvider rsa = keyPair.CreateRSA())
            {
                byte[] signature = rsa.SignHash(hash, "1.3.14.3.2.26");
                Array.Reverse(signature);
                if (signature.Length != strongNameSignatureLength)
                {
                    throw new InvalidOperationException("Signature length mismatch");
                }
                stream.Seek(strongNameSignatureFileOffset, SeekOrigin.Begin);
                stream.Write(signature, 0, signature.Length);
            }

            // compute the PE checksum
            stream.Seek(0, SeekOrigin.Begin);
            int          count = (int)stream.Length / 4;
            BinaryReader br    = new BinaryReader(stream);
            long         sum   = 0;

            for (int i = 0; i < count; i++)
            {
                sum += br.ReadUInt32();
                int carry = (int)(sum >> 32);
                sum &= 0xFFFFFFFFU;
                sum += carry;
            }
            while ((sum >> 16) != 0)
            {
                sum = (sum & 0xFFFF) + (sum >> 16);
            }
            sum += stream.Length;

            // write the PE checksum, note that it is always at offset 0xD8 in the file
            ByteBuffer bb = new ByteBuffer(4);

            bb.Write((int)sum);
            stream.Seek(0xD8, SeekOrigin.Begin);
            bb.WriteTo(stream);
        }
Exemplo n.º 6
0
		private static void StrongName(Stream stream, StrongNameKeyPair keyPair, uint headerLength, uint textSectionFileOffset, uint strongNameSignatureFileOffset, uint strongNameSignatureLength)
		{
			SHA1Managed hash = new SHA1Managed();
			using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
			{
				stream.Seek(0, SeekOrigin.Begin);
				byte[] buf = new byte[8192];
				HashChunk(stream, cs, buf, (int)headerLength);
				stream.Seek(textSectionFileOffset, SeekOrigin.Begin);
				HashChunk(stream, cs, buf, (int)(strongNameSignatureFileOffset - textSectionFileOffset));
				stream.Seek(strongNameSignatureLength, SeekOrigin.Current);
				HashChunk(stream, cs, buf, (int)(stream.Length - (strongNameSignatureFileOffset + strongNameSignatureLength)));
			}
			using (RSA rsa = keyPair.CreateRSA())
			{
				RSAPKCS1SignatureFormatter sign = new RSAPKCS1SignatureFormatter(rsa);
				byte[] signature = sign.CreateSignature(hash);
				Array.Reverse(signature);
				if (signature.Length != strongNameSignatureLength)
				{
					throw new InvalidOperationException("Signature length mismatch");
				}
				stream.Seek(strongNameSignatureFileOffset, SeekOrigin.Begin);
				stream.Write(signature, 0, signature.Length);
			}

			// compute the PE checksum
			stream.Seek(0, SeekOrigin.Begin);
			int count = (int)stream.Length / 4;
			BinaryReader br = new BinaryReader(stream);
			long sum = 0;
			for (int i = 0; i < count; i++)
			{
				sum += br.ReadUInt32();
				int carry = (int)(sum >> 32);
				sum &= 0xFFFFFFFFU;
				sum += carry;
			}
			while ((sum >> 16) != 0)
			{
				sum = (sum & 0xFFFF) + (sum >> 16);
			}
			sum += stream.Length;

			// write the PE checksum, note that it is always at offset 0xD8 in the file
			ByteBuffer bb = new ByteBuffer(4);
			bb.Write((int)sum);
			stream.Seek(0xD8, SeekOrigin.Begin);
			bb.WriteTo(stream);
		}