|
|
@@ -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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+}
|