public byte[] Hash(string fileName) { FileStream fileStream = File.OpenRead(fileName); StrongName.StrongNameSignature strongNameSignature = this.StrongHash(fileStream, StrongName.StrongNameOptions.Metadata); fileStream.Close(); return(strongNameSignature.Hash); }
public bool Verify(Stream stream) { StrongName.StrongNameSignature strongNameSignature = this.StrongHash(stream, StrongName.StrongNameOptions.Signature); if (strongNameSignature.Hash == null) { return(false); } bool result; try { AssemblyHashAlgorithm algorithm = AssemblyHashAlgorithm.SHA1; if (this.tokenAlgorithm == "MD5") { algorithm = AssemblyHashAlgorithm.MD5; } result = StrongName.Verify(this.rsa, algorithm, strongNameSignature.Hash, strongNameSignature.Signature); } catch (CryptographicException) { result = false; } return(result); }
internal StrongName.StrongNameSignature StrongHash(Stream stream, StrongName.StrongNameOptions options) { StrongName.StrongNameSignature strongNameSignature = new StrongName.StrongNameSignature(); HashAlgorithm hashAlgorithm = HashAlgorithm.Create(this.TokenAlgorithm); CryptoStream cryptoStream = new CryptoStream(Stream.Null, hashAlgorithm, CryptoStreamMode.Write); byte[] array = new byte[128]; stream.Read(array, 0, 128); if (BitConverterLE.ToUInt16(array, 0) != 23117) { return(null); } uint num = BitConverterLE.ToUInt32(array, 60); cryptoStream.Write(array, 0, 128); if (num != 128u) { byte[] array2 = new byte[num - 128u]; stream.Read(array2, 0, array2.Length); cryptoStream.Write(array2, 0, array2.Length); } byte[] array3 = new byte[248]; stream.Read(array3, 0, 248); if (BitConverterLE.ToUInt32(array3, 0) != 17744u) { return(null); } if (BitConverterLE.ToUInt16(array3, 4) != 332) { return(null); } byte[] src = new byte[8]; Buffer.BlockCopy(src, 0, array3, 88, 4); Buffer.BlockCopy(src, 0, array3, 152, 8); cryptoStream.Write(array3, 0, 248); ushort num2 = BitConverterLE.ToUInt16(array3, 6); int num3 = (int)(num2 * 40); byte[] array4 = new byte[num3]; stream.Read(array4, 0, num3); cryptoStream.Write(array4, 0, num3); uint r = BitConverterLE.ToUInt32(array3, 232); uint num4 = this.RVAtoPosition(r, (int)num2, array4); int num5 = (int)BitConverterLE.ToUInt32(array3, 236); byte[] array5 = new byte[num5]; stream.Position = (long)((ulong)num4); stream.Read(array5, 0, num5); uint r2 = BitConverterLE.ToUInt32(array5, 32); strongNameSignature.SignaturePosition = this.RVAtoPosition(r2, (int)num2, array4); strongNameSignature.SignatureLength = BitConverterLE.ToUInt32(array5, 36); uint r3 = BitConverterLE.ToUInt32(array5, 8); strongNameSignature.MetadataPosition = this.RVAtoPosition(r3, (int)num2, array4); strongNameSignature.MetadataLength = BitConverterLE.ToUInt32(array5, 12); if (options == StrongName.StrongNameOptions.Metadata) { cryptoStream.Close(); hashAlgorithm.Initialize(); byte[] array6 = new byte[strongNameSignature.MetadataLength]; stream.Position = (long)((ulong)strongNameSignature.MetadataPosition); stream.Read(array6, 0, array6.Length); strongNameSignature.Hash = hashAlgorithm.ComputeHash(array6); return(strongNameSignature); } for (int i = 0; i < (int)num2; i++) { uint num6 = BitConverterLE.ToUInt32(array4, i * 40 + 20); int num7 = (int)BitConverterLE.ToUInt32(array4, i * 40 + 16); byte[] array7 = new byte[num7]; stream.Position = (long)((ulong)num6); stream.Read(array7, 0, num7); if (num6 <= strongNameSignature.SignaturePosition && (ulong)strongNameSignature.SignaturePosition < (ulong)num6 + (ulong)((long)num7)) { int num8 = (int)(strongNameSignature.SignaturePosition - num6); if (num8 > 0) { cryptoStream.Write(array7, 0, num8); } strongNameSignature.Signature = new byte[strongNameSignature.SignatureLength]; Buffer.BlockCopy(array7, num8, strongNameSignature.Signature, 0, (int)strongNameSignature.SignatureLength); Array.Reverse(strongNameSignature.Signature); int num9 = (int)((long)num8 + (long)((ulong)strongNameSignature.SignatureLength)); int num10 = num7 - num9; if (num10 > 0) { cryptoStream.Write(array7, num9, num10); } } else { cryptoStream.Write(array7, 0, num7); } } cryptoStream.Close(); strongNameSignature.Hash = hashAlgorithm.Hash; return(strongNameSignature); }