GenerateHash() public method

public GenerateHash ( HashAlgorithm hashAlg ) : byte[]
hashAlg System.Security.Cryptography.HashAlgorithm
return byte[]
		public void FixtureSetUp () 
		{
			Assembly a = Assembly.GetExecutingAssembly ();
			hashEvidence = new Hash (a);

			md5 = MD5.Create ();
			digestMd5 = hashEvidence.GenerateHash (md5);

			sha1 = SHA1.Create ();
			digestSha1 = hashEvidence.GenerateHash (sha1);
		}
Example #2
0
        // Implement the IMembership interface.
        public bool Check(Evidence evidence)
        {
            if (evidence == null)
            {
                return(false);
            }
            IEnumerator e = evidence.GetHostEnumerator();

            while (e.MoveNext())
            {
                Hash hash = (e.Current as Hash);
                if (hash != null)
                {
                    byte[] computed = hash.GenerateHash(hashAlg);
                    if (computed == null || value.Length != computed.Length)
                    {
                        continue;
                    }
                    int posn;
                    for (posn = 0; posn < computed.Length; ++posn)
                    {
                        if (computed[posn] != value[posn])
                        {
                            break;
                        }
                    }
                    if (posn >= computed.Length)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #3
0
 bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence)
 {
     usedEvidence = null;
     if (evidence != null)
     {
         Hash hostEvidence = evidence.GetHostEvidence <Hash>();
         if (hostEvidence != null)
         {
             if ((this.m_value == null) && (this.m_element != null))
             {
                 this.ParseHashValue();
             }
             if ((this.m_hashAlg == null) && (this.m_element != null))
             {
                 this.ParseHashAlgorithm();
             }
             byte[] first = null;
             lock (this.InternalSyncObject)
             {
                 first = hostEvidence.GenerateHash(this.m_hashAlg);
             }
             if ((first != null) && CompareArrays(first, this.m_value))
             {
                 usedEvidence = hostEvidence;
                 return(true);
             }
         }
     }
     return(false);
 }
Example #4
0
        // Token: 0x06002AD1 RID: 10961 RVA: 0x0009EEE4 File Offset: 0x0009D0E4
        bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence)
        {
            usedEvidence = null;
            if (evidence == null)
            {
                return(false);
            }
            Hash hostEvidence = evidence.GetHostEvidence <Hash>();

            if (hostEvidence != null)
            {
                if (this.m_value == null && this.m_element != null)
                {
                    this.ParseHashValue();
                }
                if (this.m_hashAlg == null && this.m_element != null)
                {
                    this.ParseHashAlgorithm();
                }
                byte[] array = null;
                object internalSyncObject = this.InternalSyncObject;
                lock (internalSyncObject)
                {
                    array = hostEvidence.GenerateHash(this.m_hashAlg);
                }
                if (array != null && HashMembershipCondition.CompareArrays(array, this.m_value))
                {
                    usedEvidence = hostEvidence;
                    return(true);
                }
            }
            return(false);
        }
Example #5
0
 public static void HashCallMethods()
 {
     Hash hash = new Hash(Reflection.Assembly.Load(new Reflection.AssemblyName("System.Reflection")));
     byte[] barr = hash.GenerateHash(Cryptography.SHA1.Create());
     string str = hash.ToString();
     hash = Hash.CreateMD5(new byte[1]);
     hash = Hash.CreateSHA1(new byte[1]);
 }
Example #6
0
 private void GenerateDefaultHashes(byte[] assemblyBytes)
 {
     Type[] typeArray = new Type[3] {
         Hash.GetHashIndexType(typeof(System.Security.Cryptography.SHA1)), Hash.GetHashIndexType(typeof(System.Security.Cryptography.SHA256)), Hash.GetHashIndexType(typeof(System.Security.Cryptography.MD5))
     };
     foreach (Type index in typeArray)
     {
         Type hashImplementation = Hash.GetDefaultHashImplementation(index);
         if (hashImplementation != (Type)null && !this.m_hashes.ContainsKey(index))
         {
             this.m_hashes[index] = Hash.GenerateHash(hashImplementation, assemblyBytes);
         }
     }
 }
        // Token: 0x06002ABC RID: 10940 RVA: 0x0009E9D0 File Offset: 0x0009CBD0
        private byte[] GenerateHash(Type hashType)
        {
            Type hashIndexType = Hash.GetHashIndexType(hashType);

            byte[] array = null;
            if (!this.m_hashes.TryGetValue(hashIndexType, out array))
            {
                if (this.m_assembly == null)
                {
                    throw new InvalidOperationException(Environment.GetResourceString("Security_CannotGenerateHash"));
                }
                array = Hash.GenerateHash(hashType, this.GetRawData());
                this.m_hashes[hashIndexType] = array;
            }
            return(array);
        }
 // Token: 0x06002ABF RID: 10943 RVA: 0x0009EA8C File Offset: 0x0009CC8C
 private void GenerateDefaultHashes(byte[] assemblyBytes)
 {
     Type[] array = new Type[]
     {
         Hash.GetHashIndexType(typeof(SHA1)),
         Hash.GetHashIndexType(typeof(SHA256)),
         Hash.GetHashIndexType(typeof(MD5))
     };
     foreach (Type type in array)
     {
         Type defaultHashImplementation = Hash.GetDefaultHashImplementation(type);
         if (defaultHashImplementation != null && !this.m_hashes.ContainsKey(type))
         {
             this.m_hashes[type] = Hash.GenerateHash(defaultHashImplementation, assemblyBytes);
         }
     }
 }
Example #9
0
        /// <summary>Determines whether the specified evidence satisfies the membership condition.</summary>
        /// <returns>true if the specified evidence satisfies the membership condition; otherwise, false.</returns>
        /// <param name="evidence">The evidence set against which to make the test. </param>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
        ///   <IPermission class="System.Security.Permissions.KeyContainerPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public bool Check(Evidence evidence)
        {
            if (evidence == null)
            {
                return(false);
            }
            IEnumerator hostEnumerator = evidence.GetHostEnumerator();

            while (hostEnumerator.MoveNext())
            {
                object obj  = hostEnumerator.Current;
                Hash   hash = obj as Hash;
                if (hash != null)
                {
                    if (this.Compare(this.hash_value, hash.GenerateHash(this.hash_algorithm)))
                    {
                        return(true);
                    }
                    break;
                }
            }
            return(false);
        }
Example #10
0
        bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence)
        {
            usedEvidence = null;

            if (evidence == null)
            {
                return(false);
            }

            Hash hash = evidence.GetHostEvidence <Hash>();

            if (hash != null)
            {
                if (m_value == null && m_element != null)
                {
                    ParseHashValue();
                }

                if (m_hashAlg == null && m_element != null)
                {
                    ParseHashAlgorithm();
                }

                byte[] asmHash = null;
                lock (InternalSyncObject) {
                    asmHash = hash.GenerateHash(m_hashAlg);
                }

                if (asmHash != null && CompareArrays(asmHash, m_value))
                {
                    usedEvidence = hash;
                    return(true);
                }
            }

            return(false);
        }
Example #11
0
        //
        // Public Methods
        //

        public bool Check(Evidence evidence)
        {
            if (evidence == null)
            {
                return(false);
            }

            IEnumerator e = evidence.GetHostEnumerator();

            while (e.MoveNext())
            {
                Hash hash = (e.Current as Hash);
                if (hash == null)
                {
                    continue;
                }
                if (Compare(hash_value, hash.GenerateHash(hash_algorithm)))
                {
                    return(true);
                }
                break;
            }
            return(false);
        }
Example #12
0
		// -hash algo -hex hash
		// -hash algo -file assemblyname
		static IMembershipCondition ProcessHashMembership (string[] args, ref int i)
		{
			HashAlgorithm ha = HashAlgorithm.Create (args [++i]);
			byte [] value = null;
			switch (args [++i]) {
				case "-hex":
					value = CryptoConvert.FromHex (args [++i]);
					break;
				case "-file":
					Hash hash = new Hash (GetAssembly (args [++i]));
					value = hash.GenerateHash (ha);
					break;
				default:
					return null;
			}
			return new HashMembershipCondition (ha, value);
		}
Example #13
0
		public void GenerateHash_RIPEMD160 ()
		{
			Hash h = new Hash (wellKnownAssembly);
			byte[] result = h.GenerateHash (RIPEMD160.Create ());
			Assert.AreEqual ("29-5A-21-11-BC-CA-1B-26-A2-46-F2-48-B4-61-96-1C-A5-2C-9A-9A", BitConverter.ToString (result));
		}
Example #14
0
		[Category ("NotDotNet")] // Current hashing is compatible with Fx 2.0 but not with Fx 1.0/1.1
#endif
		public void GenerateHash_SHA512 ()
		{
			Hash h = new Hash (wellKnownAssembly);
			byte[] result = h.GenerateHash (SHA512.Create ());
			Assert.AreEqual ("C0-05-76-29-F9-E7-32-A9-8D-73-4B-90-12-6A-85-3F-5C-D3-5B-EF-A6-F2-A6-15-38-9C-22-FA-7E-72-CA-10-B0-21-E0-B6-0B-B8-AB-9E-0E-9F-64-0E-C6-F3-48-96-01-7B-A6-20-07-2F-03-6A-51-03-0F-3F-C4-70-B3-0F", BitConverter.ToString (result));
		}
Example #15
0
		[Category ("NotDotNet")] // Current hashing is compatible with Fx 2.0 but not with Fx 1.0/1.1
#endif
		public void GenerateHash_SHA384 ()
		{
			Hash h = new Hash (wellKnownAssembly);
			byte[] result = h.GenerateHash (SHA384.Create ());
			Assert.AreEqual ("23-58-E8-A2-61-B0-9E-0D-86-B1-C7-69-55-D4-52-18-5F-F8-8E-41-C1-94-B6-9B-2D-6A-6D-7F-86-9A-7F-BB-86-55-D4-BF-04-0C-B9-84-A5-52-35-9E-BC-CA-62-BC", BitConverter.ToString (result));
		}
Example #16
0
		[Category ("NotDotNet")] // Current hashing is compatible with Fx 2.0 but not with Fx 1.0/1.1
#endif
		public void GenerateHash_SHA256 ()
		{
			Hash h = new Hash (wellKnownAssembly);
			byte[] result = h.GenerateHash (SHA256.Create ());
			Assert.AreEqual ("A1-3E-D6-44-BF-1D-B2-22-5D-34-53-14-E5-26-94-78-0A-4E-D0-C2-0F-9D-BA-3D-7C-67-61-3B-3A-17-CD-B0", BitConverter.ToString (result));
		}
Example #17
0
		[Category ("NotDotNet")] // Current hashing is compatible with Fx 2.0 but not with Fx 1.0/1.1
#endif
		public void GenerateHash_SHA1 ()
		{
			Hash h = new Hash (wellKnownAssembly);
			byte[] result = h.GenerateHash (SHA1.Create ());
			Assert.AreEqual ("68-28-89-F6-D8-D4-9F-AF-FD-00-90-EF-5A-BA-C9-ED-2C-08-A6-F5", BitConverter.ToString (result));
		}
Example #18
0
		[Category ("NotDotNet")] // Current hashing is compatible with Fx 2.0 but not with Fx 1.0/1.1
#endif
		public void GenerateHash_MD5 ()
		{
			Hash h = new Hash (wellKnownAssembly);
			byte[] result = h.GenerateHash (MD5.Create ());
			Assert.AreEqual ("D0-C6-1F-66-C8-49-9F-C8-BE-63-89-9D-C5-FC-4F-D4", BitConverter.ToString (result), "GenerateHash");
		}