static byte [] HashStream(Stream stream, ImageWriter writer, out int strong_name_pointer) { const int buffer_size = 8192; var text = writer.text; var header_size = (int)writer.GetHeaderSize(); var text_section_pointer = (int)text.PointerToRawData; var strong_name_directory = writer.GetStrongNameSignatureDirectory(); if (strong_name_directory.Size == 0) { throw new InvalidOperationException(); } strong_name_pointer = (int)(text_section_pointer + (strong_name_directory.VirtualAddress - text.VirtualAddress)); var strong_name_length = (int)strong_name_directory.Size; var sha1 = new SHA1Managed(); var buffer = new byte [buffer_size]; using (var crypto_stream = new CryptoStream(Stream.Null, sha1, CryptoStreamMode.Write)) { stream.Seek(0, SeekOrigin.Begin); CopyStreamChunk(stream, crypto_stream, buffer, header_size); stream.Seek(text_section_pointer, SeekOrigin.Begin); CopyStreamChunk(stream, crypto_stream, buffer, (int)strong_name_pointer - text_section_pointer); stream.Seek(strong_name_length, SeekOrigin.Current); CopyStreamChunk(stream, crypto_stream, buffer, (int)(stream.Length - (strong_name_pointer + strong_name_length))); } return(sha1.Hash); }
static byte [] HashStream (Stream stream, ImageWriter writer, out int strong_name_pointer) { const int buffer_size = 8192; var text = writer.text; var header_size = (int) writer.GetHeaderSize (); var text_section_pointer = (int) text.PointerToRawData; var strong_name_directory = writer.GetStrongNameSignatureDirectory (); if (strong_name_directory.Size == 0) throw new InvalidOperationException (); strong_name_pointer = (int) (text_section_pointer + (strong_name_directory.VirtualAddress - text.VirtualAddress)); var strong_name_length = (int) strong_name_directory.Size; var sha1 = new SHA1Managed (); var buffer = new byte [buffer_size]; using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write)) { stream.Seek (0, SeekOrigin.Begin); CopyStreamChunk (stream, crypto_stream, buffer, header_size); stream.Seek (text_section_pointer, SeekOrigin.Begin); CopyStreamChunk (stream, crypto_stream, buffer, (int) strong_name_pointer - text_section_pointer); stream.Seek (strong_name_length, SeekOrigin.Current); CopyStreamChunk (stream, crypto_stream, buffer, (int) (stream.Length - (strong_name_pointer + strong_name_length))); } return sha1.Hash; }
private static byte[] HashStream(Stream stream, ImageWriter writer, out int strong_name_pointer) { Section text = writer.text; int headerSize = (int)writer.GetHeaderSize(); int pointerToRawData = (int)text.PointerToRawData; DataDirectory strongNameSignatureDirectory = writer.GetStrongNameSignatureDirectory(); if (strongNameSignatureDirectory.Size == 0) { throw new InvalidOperationException(); } strong_name_pointer = (int)(pointerToRawData + (strongNameSignatureDirectory.VirtualAddress - text.VirtualAddress)); int size = (int)strongNameSignatureDirectory.Size; SHA1Managed sHA1Managed = new SHA1Managed(); byte[] buffer = new byte[8192]; using (CryptoStream dest_stream = new CryptoStream(Stream.Null, sHA1Managed, CryptoStreamMode.Write)) { stream.Seek(0L, SeekOrigin.Begin); CopyStreamChunk(stream, dest_stream, buffer, headerSize); stream.Seek(pointerToRawData, SeekOrigin.Begin); CopyStreamChunk(stream, dest_stream, buffer, strong_name_pointer - pointerToRawData); stream.Seek(size, SeekOrigin.Current); CopyStreamChunk(stream, dest_stream, buffer, (int)(stream.Length - (strong_name_pointer + size))); } return(sHA1Managed.Hash); }
private static byte[] HashStream(Stream stream, ImageWriter writer, out int strong_name_pointer) { Section section = writer.text; int headerSize = (int)writer.GetHeaderSize(); int pointerToRawData = (int)section.PointerToRawData; DataDirectory strongNameSignatureDirectory = writer.GetStrongNameSignatureDirectory(); if (strongNameSignatureDirectory.Size == 0) { throw new InvalidOperationException(); } strong_name_pointer = (int)((long)pointerToRawData + (ulong)(strongNameSignatureDirectory.VirtualAddress - section.VirtualAddress)); int size = (int)strongNameSignatureDirectory.Size; SHA1Managed sHA1Managed = new SHA1Managed(); byte[] numArray = new byte[8192]; using (CryptoStream cryptoStream = new CryptoStream(Stream.Null, sHA1Managed, CryptoStreamMode.Write)) { stream.Seek((long)0, SeekOrigin.Begin); CryptoService.CopyStreamChunk(stream, cryptoStream, numArray, headerSize); stream.Seek((long)pointerToRawData, SeekOrigin.Begin); CryptoService.CopyStreamChunk(stream, cryptoStream, numArray, strong_name_pointer - pointerToRawData); stream.Seek((long)size, SeekOrigin.Current); CryptoService.CopyStreamChunk(stream, cryptoStream, numArray, (int)(stream.Length - (long)(strong_name_pointer + size))); } return(sHA1Managed.Hash); }