All pastes #76121 Raw Edit

Stuff

public text v1 · immutable
·published 2006-07-01 03:04 UTC
rendered paste body
using System;
using System.Runtime.InteropServices;
using System.Reflection;

namespace StormTide.Stream.Manager
{
	/// <summary>
	/// Verifies an assembly strong name, and guarantees that it is not overriden.
	/// Also guarantees that the code is not modified.
	/// 
	/// Also contacts the certificate authority and checks the validity of the
	/// public key.
	/// </summary>
	public sealed class StrongNameValidate
	{
		[DllImport("mscoree.dll", CharSet=CharSet.Unicode)]
		static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool  pfWasVerified);
		
		private Assembly _assembly;
		public StrongNameValidate(Assembly assembly)
		{
			this._assembly = assembly;
		}
		public bool Validate() {
			Assembly assembly = this._assembly;

			bool notForced = false;
			string assemblyPathURN = ((Assembly) assembly).CodeBase;
			string assemblyPath = assemblyPathURN.Replace("file:///","").Replace('/','\\');

			bool verified = StrongNameSignatureVerificationEx(assemblyPath, true, ref notForced);

			return verified & notForced;
		}
		public bool CheckLicensed() {
			Assembly assembly = this._assembly;
			byte[] publicKeyBytes = ((AssemblyName) assembly.GetName()).GetPublicKey();
			string publicKey = Convert.ToBase64String(publicKeyBytes);
			return true;
			//TODO: Add centralized server certificate validation.
		}
	}
}