コード例 #1
0
        private static void Append(this HashAlgorithm algorithm, Attribute value, bool isFinalAppend = false)
        {
            algorithm.Append(AttributePrefix);
            Type attributeType = value.GetType();

            algorithm.Append(attributeType, AttributeBehavior.Exclude, isFinalAppend);
        }
コード例 #2
0
 public static void Append(this HashAlgorithm algorithm, AssemblyName value, bool isFinalAppend = false)
 {
     algorithm.Append(AssemblyNamePrefix);
     algorithm.Append(value.Name);
     algorithm.Append(value.FullName);
     algorithm.Append(value.Version.ToString(), isFinalAppend);
 }
コード例 #3
0
 private void Hash(HashAlgorithm algorithm, bool isFinalAppend)
 {
     algorithm.Append(Name);
     algorithm.AppendType(Type);
     // TODO Find a way to include the serializer and deserializer in the identity hash
     //algorithm.Append(Serializer);
     //algorithm.Append(Deserializer);
     algorithm.Append(Id.Id, isFinalAppend);
 }
コード例 #4
0
        private void HashInternal(byte[] value)
        {
            if (digest != null)
            {
                // we started to hash some data. If this instance was previously
                // used, let's wipe the cached digest value
                digest = null;
#if !NETFX_CORE
                if (!hash.CanReuseTransform)
                {
                    throw new InvalidOperationException("This hash algorithm cannot be reused.");
                }
                hash.Initialize();
#endif // NETFX_CORE
            }
#if NETFX_CORE
            hash.Append(CryptographicBuffer.CreateFromByteArray(value));
#else
            // DEBUG -- useful for seeing all bytes that get hashed, when debugging two implementations that don't interop
            //System.Diagnostics.Debug.Write("Hashing: ");
            //for (int i = 0; i < value.Length; i++)
            //    System.Diagnostics.Debug.Write(value[i] + ",");
            //System.Diagnostics.Debug.WriteLine("");
            // END DEBUG

            hash.TransformBlock(value, 0, value.Length, value, 0);
#endif // NETFX_CORE
        }
コード例 #5
0
        private static void Append(this HashAlgorithm algorithm, PropertyInfo value, bool isFinalAppend = false)
        {
            algorithm.Append(PropertyPrefix);
            algorithm.AppendMemberInfo(value, AttributeBehavior.Include);
            var indexParameters = value.GetIndexParameters();

            foreach (var i in indexParameters)
            {
                algorithm.Append(i);
            }
            algorithm.Append(indexParameters.Length);

            if (value.GetMethod == null)
            {
                algorithm.Append(ExplicitNull);
            }
            else
            {
                algorithm.Append(value.GetMethod);
            }
            if (value.SetMethod == null)
            {
                algorithm.Append(ExplicitNull);
            }
            else
            {
                algorithm.Append(value.SetMethod);
            }

            algorithm.AppendType(value.PropertyType, isFinalAppend);
        }
コード例 #6
0
        private static void AppendMemberInfo(this HashAlgorithm algorithm, MemberInfo value, AttributeBehavior attributeBehavior, bool isFinalAppend = false)
        {
            algorithm.Append((int)value.MemberType);

            int attributeCount = 0;

            if (attributeBehavior == AttributeBehavior.Include)
            {
                foreach (var a in value.GetCustomAttributes <Attribute>())
                {
                    algorithm.Append(a);
                    ++attributeCount;
                }
            }
            // Always add the attribute count. This way a type without any attributes hashes the same no matter what the parameter says
            algorithm.Append(attributeCount);

            algorithm.Append(value.Name, isFinalAppend);
        }
コード例 #7
0
        /// <summary>
        /// Calculates a hash of a Delegate object. The resulting hash can be used to validate that two different Delegate instanses
        /// are setup in the same way (meaning they have the same type of targets and in the same order).
        /// The hash value is also stable outside of the current process lifetime, so it can be used to validate saved data.
        /// </summary>
        /// <param name="algorithm"></param>
        /// <param name="value"></param>
        /// <param name="isFinalAppend"></param>
        public static void Append(this HashAlgorithm algorithm, Delegate value, bool isFinalAppend = false)
        {
            algorithm.Append(DelegatePrefix);
            var invocationList = value.GetInvocationList();

            foreach (var d in invocationList)
            {
                if (d.Target == null)
                {
                    algorithm.Append(ExplicitNull);
                }
                else
                {
                    algorithm.AppendType(d.Target.GetType());
                }
                algorithm.Append(d.Method);
            }
            algorithm.Append(invocationList.Length, isFinalAppend);
        }
コード例 #8
0
            public IContext EndSetup()
            {
                // Finish the hashing if it's enabled
                if (payloadHashAlgorithm != null)
                {
                    payloadHashAlgorithm.Append(payloads.Count, true);
                }

                return(new DefaultContext(contextName, payloads));
            }
コード例 #9
0
        private static void Append(this HashAlgorithm algorithm, MethodInfo value, bool isFinalAppend = false)
        {
            algorithm.Append(MethodPrefix);
            algorithm.AppendMemberInfo(value, AttributeBehavior.Include);
            algorithm.Append((int)value.Attributes);

            if (value.IsGenericMethod)
            {
                algorithm.Append(ExplicitTrue);
                if (value.IsGenericMethodDefinition)
                {
                    algorithm.Append(ExplicitTrue);
                }
                else
                {
                    algorithm.Append(ExplicitFalse);
                }

                var genericArguments = value.GetGenericArguments();
                foreach (var arg in genericArguments)
                {
                    algorithm.AppendType(arg);
                }
                algorithm.Append(genericArguments.Length);
            }
            else
            {
                algorithm.Append(ExplicitFalse);
            }

            var parameters = value.GetParameters();

            foreach (var p in parameters)
            {
                algorithm.Append(p);
            }
            algorithm.Append(parameters.Length);

            algorithm.AppendType(value.ReturnType, isFinalAppend);
        }
コード例 #10
0
        private static void Append(this HashAlgorithm algorithm, ParameterInfo value, bool isFinalAppend = false)
        {
            algorithm.Append(ParameterPrefix);
            algorithm.Append((int)value.Attributes);
            algorithm.Append(value.Name);
            algorithm.Append(value.Position);
            int attributeCount = 0;

            foreach (var a in value.GetCustomAttributes <Attribute>())
            {
                algorithm.Append(a);
                ++attributeCount;
            }
            algorithm.Append(attributeCount);
            algorithm.AppendType(value.ParameterType, isFinalAppend);
        }
コード例 #11
0
        /// <summary>
        /// If two class types have the same name, interface, and attributes (not including the values in the attribute), they generate the same hash.
        /// This does *NOT* mean that they are implemented the same, just that they look the same on the surface.
        /// </summary>
        /// <param name="algorithm"></param>
        /// <param name="value"></param>
        /// <param name="isFinalAppend"></param>
        public static void AppendClass(this HashAlgorithm algorithm, Type value, bool isFinalAppend = false)
        {
            algorithm.Append(ClassPrefix);
            var methods = value.GetMethods();

            foreach (var m in methods)
            {
                algorithm.Append(m);
            }
            algorithm.Append(methods.Length);

            var properties = value.GetProperties();

            foreach (var p in properties)
            {
                algorithm.Append(p);
            }
            algorithm.Append(properties.Length);

            algorithm.Append(value, AttributeBehavior.Include, isFinalAppend);
        }
コード例 #12
0
        public static void Append(this HashAlgorithm algorithm, long value, bool isFinalAppend = false)
        {
            var bytes = BitConverter.GetBytes(value);

            algorithm.Append(bytes, isFinalAppend);
        }
コード例 #13
0
        public static void Append(this HashAlgorithm algorithm, string value, bool isFinalAppend = false)
        {
            var bytes = Encoding.UTF8.GetBytes(value);

            algorithm.Append(bytes, isFinalAppend);
        }
コード例 #14
0
 public static byte[] CalculateHash(this HashAlgorithm algorithm, long value)
 {
     algorithm.Initialize();
     algorithm.Append(value, true);
     return(algorithm.Hash);
 }
コード例 #15
0
        private static void Append(this HashAlgorithm algorithm, Type value, AttributeBehavior attributeBehavior, bool isFinalAppend = false)
        {
            algorithm.Append(TypePrefix);
            if (value.IsGenericType)
            {
                algorithm.Append(ExplicitTrue);
            }
            else
            {
                algorithm.Append(ExplicitFalse);
            }
            if (value.IsGenericTypeDefinition)
            {
                algorithm.Append(ExplicitTrue);
            }
            else
            {
                algorithm.Append(ExplicitFalse);
            }

            // TODO Add more explicit type info for generic versions
            if (value.IsGenericParameter)
            {
                algorithm.Append(ExplicitTrue);
            }
            else
            {
                algorithm.Append(ExplicitFalse);
            }

            if (value.ContainsGenericParameters)
            {
                algorithm.Append(ExplicitTrue);
            }
            else
            {
                algorithm.Append(ExplicitFalse);
            }


            if (value.IsArray)
            {
                algorithm.Append(ExplicitTrue);
                algorithm.AppendType(value.GetElementType());
            }
            else
            {
                algorithm.Append(ExplicitFalse);
            }

            algorithm.AppendMemberInfo(value, attributeBehavior, isFinalAppend);
        }
コード例 #16
0
 /// <summary>
 /// Calculates a hash of the specified type.
 /// </summary>
 /// <param name="algorithm"></param>
 /// <param name="value"></param>
 /// <param name="isFinalAppend"></param>
 public static void AppendType(this HashAlgorithm algorithm, Type value, bool isFinalAppend = false)
 {
     algorithm.Append(value, AttributeBehavior.Include, isFinalAppend);
 }