public void HashTest()
        {
            var inputHex    = "74657374206f66206d6163";
            var expectedHex = "9ff3e52fa31c9e0fa0b08e19c40591553ea64b73709633271975bfab2db9d980";

            var inputBytes    = CallbackUtils.GetBytesFromHex(inputHex);
            var expectedBytes = CallbackUtils.GetBytesFromHex(expectedHex);

            var resultBytes = HashCallback.CalculateHash(inputBytes);

            resultBytes.Should().Equal(expectedBytes);
        }
        public Thread CalcHash(string filepath, HashCallback callback)
        {
            Thread th = new Thread(() =>
            {
                byte[] hash = new byte[32];
                if (!fileHash(filepath, hash))
                {
                    callback?.Invoke(null);
                    return;
                }
                callback?.Invoke(hash);
            });

            th.Start();
            return(th);
        }
Exemple #3
0
        public void HashTest()
        {
            var inputHex    = "74657374206f66206d6163";
            var expectedHex = "9ff3e52fa31c9e0fa0b08e19c40591553ea64b73709633271975bfab2db9d980";

            var inputBytes    = CallbackUtils.GetBytesFromHex(inputHex);
            var expectedBytes = CallbackUtils.GetBytesFromHex(expectedHex);

#if !NETCOREAPP1_1
            var resultBytes = HashCallback.CalculateHash(inputBytes);
            resultBytes.Should().Equal(expectedBytes);
#else
            var exception = Record.Exception(() => HashCallback.CalculateHash(inputBytes));
            exception.Should().BeOfType <System.PlatformNotSupportedException>();
#endif
        }
Exemple #4
0
        // HashStructure takes an interface and hashes all the values within
        // the structure. Only _values_ are hashed: keys of objects are not.
        //
        // For the HashCallback, see the built-in HashCallbacks below.
        //~ func HashStructure(s interface{}, cb HashCallback) (interface{}, error) {
        public static T Hash <T>(T s, HashCallback cb)
        {
            //~ s, err:= copystructure.Copy(s)
            //~ if err != nil {
            //~     return nil, err
            //~ }

            object obj = s;

            if (obj == null)
            {
                return(s);
            }

            switch (s)
            {
            case IDictionary <string, object> d:
                s = (T)Activator.CreateInstance(typeof(Dictionary <string, object>), d);
                break;

            default:
                throw new NotSupportedException($"unhandled structure type [{typeof(T).FullName}]");
            }

            //~ walker := &hashWalker{Callback: cb}
            //~ if err := reflectwalk.Walk(s, walker); err != nil {
            //~     return nil, err
            //~ }
            var walker = new HashWalker {
                Callback = cb,
            };

            ReflectWalk.Walk(s, walker);

            return(s);
        }
Exemple #5
0
        // Hash will hash the given type. This has built-in support for auth,
        // requests, and responses. If it is a type that isn't recognized, then
        // it will be passed through.
        //
        // The structure is modified in-place.
        //~ func Hash(salter *salt.Salt, raw interface{}) error {
        public static void Hash(Salt salter, object raw)
        {
            HashCallback fn = salter.GetIdentifiedHMAC;

            //~ switch s := raw.(type) {
            switch (raw)
            {
            //~ case *logical.Auth:
            //~     if s == nil {
            //~         return nil
            //~     }
            //~     if s.ClientToken != "" {
            //~         s.ClientToken = fn(s.ClientToken)
            //~     }
            //~     if s.Accessor != "" {
            //~         s.Accessor = fn(s.Accessor)
            //~     }
            case Logical.Auth s:
                if (s == null)
                {
                    return;
                }
                if (!string.IsNullOrEmpty(s.ClientToken))
                {
                    s.ClientToken = fn(s.ClientToken);
                }
                if (!string.IsNullOrEmpty(s.Accessor))
                {
                    s.Accessor = fn(s.Accessor);
                }
                break;

            //~ case *logical.Request:
            case Logical.Request s:
                //~ if s == nil {
                //~     return nil
                //~ }
                //~ if s.Auth != nil {
                //~     if err := Hash(salter, s.Auth); err != nil {
                //~         return err
                //~     }
                //~ }
                if (s == null)
                {
                    return;
                }
                if (s.Auth != null)
                {
                    Hash(salter, s.Auth);
                }

                //~ if s.ClientToken != "" {
                //~     s.ClientToken = fn(s.ClientToken)
                //~ }
                if (!string.IsNullOrEmpty(s.ClientToken))
                {
                    s.ClientToken = fn(s.ClientToken);
                }

                //~ if s.ClientTokenAccessor != "" {
                //~     s.ClientTokenAccessor = fn(s.ClientTokenAccessor)
                //~ }
                if (!string.IsNullOrEmpty(s.ClientTokenAccessor))
                {
                    s.ClientTokenAccessor = fn(s.ClientTokenAccessor);
                }

                //~ data, err := HashStructure(s.Data, fn)
                //~ if err != nil {
                //~     return err
                //~ }
                //~
                //~ s.Data = data.(map[string]interface{})
                s.Data = Hash(s.Data, fn);
                break;

            //~ case *logical.Response:
            case Logical.Response s:
                //~ if s == nil {
                //~     return nil
                //~ }
                if (s == null)
                {
                    return;
                }

                //~ if s.Auth != nil {
                //~     if err := Hash(salter, s.Auth); err != nil {
                //~         return err
                //~     }
                //~ }
                if (s.Auth != null)
                {
                    Hash(salter, s.Auth);
                }

                //~ if s.WrapInfo != nil {
                //~     if err := Hash(salter, s.WrapInfo); err != nil {
                //~         return err
                //~     }
                //~ }
                if (s.WrapInfo != null)
                {
                    Hash(salter, s.WrapInfo);
                }

                //~ data, err := HashStructure(s.Data, fn)
                //~ if err != nil {
                //~     return err
                //~ }
                //~
                //~ s.Data = data.(map[string]interface{})
                s.Data = Hash(s.Data, fn);
                break;

            //~ case *logical.ResponseWrapInfo:
            case Logical.ResponseWrapInfo s:
                //~ if s == nil {
                //~     return nil
                //~ }
                //~
                //~ s.Token = fn(s.Token)
                //~
                //~ if s.WrappedAccessor != "" {
                //~     s.WrappedAccessor = fn(s.WrappedAccessor)
                //~ }
                if (s == null)
                {
                    return;
                }
                s.Token = fn(s.Token);
                if (!string.IsNullOrEmpty(s.WrappedAccessor))
                {
                    s.WrappedAccessor = fn(s.WrappedAccessor);
                }
                break;
            }

            //~ return nil
        }