Browse Source

2003-01-05 Sebastien Pouliot <[email protected]>

	* Hash.cs: Added [Serializable] and ISerializable to class declaration.
	Changed how the hash algorithm are created (now use system default
	implementation). Added some exception handling.

svn path=/trunk/mcs/; revision=10189
Sebastien Pouliot 23 years ago
parent
commit
620189bcbf

+ 10 - 0
mcs/class/corlib/System.Security.Policy/ChangeLog

@@ -1,7 +1,17 @@
+2003-01-05  Sebastien Pouliot  <[email protected]>
+
+	* Hash.cs: Added [Serializable] and ISerializable to class declaration.
+	Changed how the hash algorithm are created (now use system default 
+	implementation). Added some exception handling.
+
 2002-12-20 Jackson Harper <[email protected]>
 
 	* Evidence.cs: Copy constructor does not attempt to merge if it recieves a null ref
 
+2002-12-16  Sebastien Pouliot  <[email protected]>
+
+	* StrongName.cs: New. Implemented.
+
 2002-12-15  Sebastien Pouliot  <[email protected]>
 
 	* Publisher.cs: New. Implemented.

+ 80 - 60
mcs/class/corlib/System.Security.Policy/Hash.cs

@@ -1,9 +1,13 @@
+//
 // System.Security.Policy.Hash
 //
-// Author(s):
-//  Jackson Harper ([email protected])
+// Authors:
+//	Jackson Harper ([email protected])
+//	Sebastien Pouliot ([email protected])
 //
 // (C) 2002 Jackson Harper, All rights reserved.
+// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
 
 using System;
 using System.IO;
@@ -14,76 +18,92 @@ using System.Security.Cryptography;
 
 namespace System.Security.Policy {
 
-	[MonoTODO]
-	public sealed class Hash {
-		
-		private Assembly assembly;
-		private byte[] data = null;
+[Serializable]
+public sealed class Hash : ISerializable {
 
-		public Hash(Assembly assembly)
-		{
-			this.assembly = assembly;
-		}
+	private Assembly assembly;
+	private byte[] data = null;
 
-		//
-		// Public Properties
-		//
-		
-		public byte[] MD5 
-		{
-			get {
-				return GenerateHash (new MD5CryptoServiceProvider ());
-			}
+	public Hash (Assembly assembly) 
+	{
+		if (assembly == null)
+			throw new ArgumentNullException ("assembly");
+		this.assembly = assembly;
+	}
+
+	//
+	// Public Properties
+	//
+
+	public byte[] MD5 {
+		get {
+			// fully named to avoid conflit between MD5 property and class name
+			HashAlgorithm hash = System.Security.Cryptography.MD5.Create ();
+			return GenerateHash (hash);
 		}
+	}
 
-		public byte[] SHA1
-		{
-			get {
-				return GenerateHash (new SHA1CryptoServiceProvider ());
-			}
+	public byte[] SHA1 {
+		get {
+			// fully named to avoid conflit between SHA1 property and class name
+			HashAlgorithm hash = System.Security.Cryptography.SHA1.Create ();
+			return GenerateHash (hash);
 		}
+	}
 
-		//
-		// Public Methods
-		//
+	//
+	// Public Methods
+	//
+
+	public byte[] GenerateHash (HashAlgorithm hashAlg) 
+	{
+		if (hashAlg == null)
+			throw new ArgumentNullException ("hashAlg");
+		return hashAlg.ComputeHash (GetData ());
+	}
+
+	[MonoTODO]
+	public void GetObjectData (SerializationInfo info, StreamingContext context) 
+	{
+		if (info == null)
+			throw new ArgumentNullException ("info");
+		throw new NotImplementedException ();
+	}
+
+	[MonoTODO("The Raw data seems to be different than the raw data I have")]
+	public override string ToString () 
+	{
+		SecurityElement se = new SecurityElement (GetType ().FullName);
+		se.AddAttribute ("version", "1");
 		
-		public byte[] GenerateHash(HashAlgorithm hashAlg)
-		{
-			return hashAlg.ComputeHash (GetData ());
-		}
+		StringBuilder sb = new StringBuilder ();
+		byte[] raw = GetData ();
+		for (int i=0; i < raw.Length; i++)
+			sb.Append (raw [i].ToString ("X2"));
 
-		[MonoTODO]
-		public void GetObjectData(SerializationInfo info, StreamingContext context)
-		{
-			throw new NotImplementedException ();
-		}
+		se.AddChild (new SecurityElement ("RawData", sb.ToString ()));
+		return se.ToString ();
+	}
 
-		[MonoTODO("The Raw data seems to be different than the raw data I have")]
-		public override string ToString()
-		{
-			SecurityElement se = new SecurityElement (GetType ().FullName);
-			se.AddAttribute ("version", "1");
-			se.AddChild (new SecurityElement ("RawData", GetData ().ToString ()));
+	//
+	// Private Methods
+	//
 
-			return se.ToString ();
+	[MonoTODO("This doesn't match the MS version perfectly.")]
+	private byte[] GetData () 
+	{
+		if (null == data) {
+			// TODO we mustn't hash the complete assembly!
+			// ---- Look at ToString (MS version) for what to hash (and what not to)
+			// TODO we must drop the authenticode signature (if present)
+			FileStream stream = new 
+				FileStream (assembly.Location, FileMode.Open, FileAccess.Read);
+			data = new byte [stream.Length];
+			stream.Read (data, 0, (int)stream.Length);
 		}
 
-		//
-		// Private Methods
-		//
-
-		[MonoTODO("This doesn't match the MS version perfectly.")]
-		private byte[] GetData()
-		{
-			if (null == data) {
-				FileStream stream = new 
-					FileStream( assembly.Location, FileMode.Open, FileAccess.Read );
-				data = new byte[stream.Length];
-				stream.Read( data, 0, (int)stream.Length );
-			}
-
-			return data;
-		}
+		return data;
 	}
 }
 
+}