示例#1
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 = CryptoHack.CreateRSA(keyPair))
            {
                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);
        }
示例#2
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);
        }
示例#3
0
 internal void Write(ByteBuffer bb)
 {
     bb.WriteTo(stream);
 }
示例#4
0
 internal void Write(ByteBuffer bb)
 {
     bb.WriteTo(stream);
 }
		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);
		}
示例#6
0
		private static void StrongName(FileStream fs, StrongNameKeyPair keyPair, uint headerLength, uint textSectionFileOffset, uint strongNameSignatureFileOffset, uint strongNameSignatureLength)
		{
			SHA1Managed hash = new SHA1Managed();
			using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
			{
				fs.Seek(0, SeekOrigin.Begin);
				byte[] buf = new byte[8192];
				HashChunk(fs, cs, buf, (int)headerLength);
				fs.Seek(textSectionFileOffset, SeekOrigin.Begin);
				HashChunk(fs, cs, buf, (int)(strongNameSignatureFileOffset - textSectionFileOffset));
				fs.Seek(strongNameSignatureLength, SeekOrigin.Current);
				HashChunk(fs, cs, buf, (int)(fs.Length - (strongNameSignatureFileOffset + strongNameSignatureLength)));
			}
			using (RSA rsa = CryptoHack.CreateRSA(keyPair))
			{
				RSAPKCS1SignatureFormatter sign = new RSAPKCS1SignatureFormatter(rsa);
				sign.SetHashAlgorithm("SHA1");
				byte[] signature = sign.CreateSignature(hash.Hash);
				Array.Reverse(signature);
				Debug.Assert(signature.Length == strongNameSignatureLength);
				fs.Seek(strongNameSignatureFileOffset, SeekOrigin.Begin);
				fs.Write(signature, 0, signature.Length);
			}

			// compute the PE checksum
			fs.Seek(0, SeekOrigin.Begin);
			int count = (int)fs.Length / 4;
			BinaryReader br = new BinaryReader(fs);
			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 += fs.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);
			fs.Seek(0xD8, SeekOrigin.Begin);
			bb.WriteTo(fs);
		}