Example #1
0
        public void InvalidResource()
        {
            var mined = HashCash.Mine("yomama", 10);

            // token is for wrong resource
            Assert.That(mined.Validate("filomon", 10), Is.False);
        }
Example #2
0
        public void ValidToken()
        {
            var mined = HashCash.Mine("yomama", 10);

            // token is for wrong resource
            Assert.That(mined.Validate("yomama", 10), Is.True);
        }
Example #3
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);
 }
Example #4
0
        public void EncodingDecoding()
        {
            byte[] buffer = new byte[1000];
            _ = HashCashEncoding.Encode(buffer, 0, hashCash);

            HashCash decoded = HashCashEncoding.Decode(buffer, 0);

            Assert.That(decoded, Is.EqualTo(hashCash));
        }
Example #5
0
 public void Setup()
 {
     hashCash = new HashCash(
         DateTime.UtcNow,
         "yomama",
         123123,
         10
         );
 }
Example #6
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));
        }
Example #7
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));
        }
Example #8
0
        internal void HandleChallengeRequest(ulong challenge, byte difficulty)
        {
            _stateLock.EnterReadLock();

            try
            {
                if (State == ConnectionState.RequestingConnection)
                {
                    LastMessageIn = NetTime.Now;

                    // Solve the hashcash
                    if (HashCash.TrySolve(challenge, difficulty, ulong.MaxValue, out ulong additionsRequired))
                    {
                        if (Logging.CurrentLogLevel <= LogLevel.Debug)
                        {
                            Logging.LogInfo("Solved the challenge");
                        }

                        // Set the solved results
                        ConnectionChallenge = challenge;
                        ChallengeDifficulty = difficulty;
                        ChallengeAnswer     = additionsRequired;

                        // Set resend values
                        HandshakeResendAttempts = 0;
                        HandshakeStarted        = NetTime.Now;
                        HandshakeLastSendTime   = NetTime.Now;
                        State = ConnectionState.SolvingChallenge;

                        // Send the response
                        SendChallengeResponse();
                    }
                    else
                    {
                        // Failed to solve
                        if (Logging.CurrentLogLevel <= LogLevel.Error)
                        {
                            Logging.LogError("Failed to solve the challenge");
                        }
                    }
                }
            }
            finally
            {
                _stateLock.ExitReadLock();
            }
        }
Example #9
0
        public void TestMiningNotGoodEnough()
        {
            var mined = HashCash.Mine("yomama", 10);

            Assert.That(mined.ValidateHash(20), Is.False);
        }
Example #10
0
        public void TestMining()
        {
            var mined = HashCash.Mine("yomama", 10);

            Assert.That(mined.ValidateHash(10), Is.True);
        }
Example #11
0
        public void TestShaDiff()
        {
            var hashCash2 = new HashCash(hashCash.dt, hashCash.resource + 1, hashCash.salt, hashCash.counter);

            Assert.That(hashCash2.Hash(), Is.Not.EqualTo(hashCash.Hash()));
        }
Example #12
0
        public void TestShaMatch()
        {
            HashCash hashCash2 = hashCash;

            Assert.That(hashCash2.Hash(), Is.EqualTo(hashCash.Hash()));
        }
Example #13
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();
            }
        }