Skip to content

Instantly share code, notes, and snippets.

@philiplaureano
Created April 19, 2013 19:31
Show Gist options
  • Save philiplaureano/5422630 to your computer and use it in GitHub Desktop.
Save philiplaureano/5422630 to your computer and use it in GitHub Desktop.
This sample shows how Tao uses hashing to verify all of its streams and byte arrays. It makes CLR metadata and bytecode verification possible since we can use it to incrementally verify small chunks of data against a stream of bytes from an assembly.
using Nemerle;
using Nemerle.Assertions;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
namespace Tao
{
public static class StreamExtensions
{
public static GetHash([NotNull] this stream : Stream) : string
{
_ = stream.Seek(0, SeekOrigin.Begin);
def reader = BinaryReader(stream);
def streamLength = stream.Length;
def bytes = reader.ReadBytes(streamLength :> int);
bytes.GetHash();
}
public static GetHash(this byteArray : array[byte]) : string
{
def provider = System.Security.Cryptography.SHA512CryptoServiceProvider();
BitConverter.ToString(provider.ComputeHash(byteArray));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment