Пример #1
0
 public void ValidHash()
 {
     byte[] hash = new byte[] {
         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
     };
     Assert.That(HashCash.Validate(hash, 15), Is.True);
 }
Пример #2
0
        public void HashCash_Validate_Should_Return_False_When_Nonce_Is_Invalid()
        {
            var hc   = new HashCash();
            var data = "test";

            var nonce = hc.GetNonce(data) - 1;

            Assert.False(hc.Validate(data, nonce));
        }
Пример #3
0
        public void HashCash_GetNonce_Should_Return_Valid_Nonce()
        {
            var hc   = new HashCash();
            var data = "test";

            var nonce = hc.GetNonce(data);

            Assert.True(hc.Validate(data, nonce));
        }
Пример #4
0
        internal void HandleChallengeResponse(ulong proposedSolution)
        {
            _stateLock.EnterUpgradeableReadLock();

            try
            {
                if (State == ConnectionState.RequestingChallenge)
                {
                    // Check if it is solved
                    bool isCollided = HashCash.Validate(ConnectionChallenge, proposedSolution, ChallengeDifficulty);

                    if (isCollided)
                    {
                        // Success, they completed the hashcash challenge

                        if (Logging.CurrentLogLevel <= LogLevel.Debug)
                        {
                            Logging.LogInfo("Client " + EndPoint + " successfully completed challenge of difficulty " + ChallengeDifficulty);
                        }

                        // Assign the channels
                        for (byte i = 0; i < Config.ChannelTypes.Length; i++)
                        {
                            Channels[i] = Socket.ChannelPool.GetChannel(Config.ChannelTypes[i], i, this, Config, MemoryManager);
                        }

                        // Reset hail status
                        HailStatus = new MessageStatus()
                        {
                            Attempts    = 0,
                            HasAcked    = false,
                            LastAttempt = NetTime.MinValue
                        };


                        _stateLock.EnterWriteLock();

                        try
                        {
                            // Change state to connected
                            State = ConnectionState.Connected;
                        }
                        finally
                        {
                            _stateLock.ExitWriteLock();
                        }

                        // Save time
                        ConnectionCompleted = NetTime.Now;

                        // Print connected
                        if (Logging.CurrentLogLevel <= LogLevel.Info)
                        {
                            Logging.LogInfo("Client " + EndPoint + " successfully connected");
                        }

                        // Send hail
                        SendHail();

                        // Send to userspace
                        Socket.PublishEvent(new NetworkEvent()
                        {
                            Connection        = this,
                            Socket            = Socket,
                            Type              = NetworkEventType.Connect,
                            AllowUserRecycle  = false,
                            ChannelId         = 0,
                            Data              = new ArraySegment <byte>(),
                            InternalMemory    = null,
                            SocketReceiveTime = NetTime.Now,
                            MemoryManager     = MemoryManager,
                            EndPoint          = EndPoint
                        });
                    }
                    else
                    {
                        // Failed, disconnect them
                        if (Logging.CurrentLogLevel <= LogLevel.Warning)
                        {
                            Logging.LogWarning("Client " + EndPoint + " failed the challenge");
                        }
                    }
                }
            }
            finally
            {
                _stateLock.ExitUpgradeableReadLock();
            }
        }