예제 #1
0
        public void Remove(T node)
        {
            _lock.EnterWriteLock();
            try
            {
                byte[] key = _keyProvider(node);
                int offset = key.Length;

                Array.Resize(ref key, offset + 1);

                for (byte i = 0; i < _replicate; i++)
                {
                    key[offset] = i;
                    var hash = (int)_hashGenerator.Hash(key);
                    if (!_circle.Remove(hash))
                        throw new Exception("can not remove a node that not added");
                }

                _keyCache = RebuildKeyCache();
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
        public T this[string key]
        {
            get
            {
                var hash = (int)_hashGenerator.Hash(key);

                return(GetNode(hash));
            }
        }
        public void Hash_AreNotEqual_ReturnsTrue()
        {
            // Arrange
            const string plainText = "test";
            var          salt      = _hashGenerator.Salt();

            // Act
            var result = _hashGenerator.Hash(plainText, salt);

            // Assert
            Assert.AreNotEqual(plainText, result);
        }
예제 #4
0
        public async Task Send(ConsumeContext <TMessage> context, IPipe <ConsumeContext <TMessage> > next)
        {
            var key = _keyProvider(context);

            if (key == null)
            {
                throw new InvalidOperationException("The key cannot be null");
            }

            var hash = _hashGenerator.Hash(key);

            var partitionId = hash % _partitionCount;

            try
            {
                Interlocked.Increment(ref _attemptCount[partitionId]);

                await _partitions[partitionId].Send(context, next);

                Interlocked.Increment(ref _successCount[partitionId]);
            }
            catch
            {
                Interlocked.Increment(ref _failureCount[partitionId]);
                throw;
            }
        }
        public async Task <User> RegisterPasswordAsync(string username, string email, string password)
        {
            if (!_regexValidator.IsValidEmail(email))
            {
                throw new InvalidEmailException();
            }

            if (!_regexValidator.IsValidPassword(password))
            {
                throw new InvalidPasswordException();
            }

            if (await _repository.ReadByUsernameAsync(username) != null)
            {
                throw new UsernameAlreadyExistsException();
            }

            if (await _repository.ReadByEmailAsync(email) != null)
            {
                throw new EmailAlreadyExistsException();
            }

            var salt           = _hashGenerator.Salt();
            var hashedPassword = _hashGenerator.Hash(password, salt);

            var user = await _repository.CreateAsync(
                new User
            {
                Username = username,
                Email    = email,
                Password = hashedPassword,
                Salt     = salt
            }
                );

            await _messageQueuePublisher.PublishMessageAsync(
                "Dwetter",
                "EmailMicroservice",
                "RegisterUser",
                JsonConvert.SerializeObject(new
            {
                email    = user.Email,
                username = user.Username
            }
                                            ));

            await _messageQueuePublisher.PublishMessageAsync(
                "Dwetter",
                "ProfileMicroservice",
                "RegisterUser",
                new
            {
                userId   = user.Id,
                username = user.Username
            }
                );

            return(user.WithoutSensitiveData());
        }
예제 #6
0
        public void Remove(T node)
        {
            _lock.EnterWriteLock();
            try
            {
                for (int i = 0; i < _replicate; i++)
                {
                    var hash = (int)_hashGenerator.Hash(_keyProvider(node) + i);
                    if (!_circle.Remove(hash))
                    {
                        throw new Exception("can not remove a node that not added");
                    }
                }

                _keyCache = RebuildKeyCache();
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
예제 #7
0
        private static Guid ToGuid(byte[] data, IHashGenerator hashGenerator)
        {
            #if DEBUG
            if (data.IsEmpty())
            {
                throw  new ArgumentNullException("data");
            }
            #endif
            if (data.IsEmpty())
            {
                return(Guid.Empty);
            }

            byte[] value       = hashGenerator.Hash(data);
            var    destination = new byte[16];
            Array.Copy(value, destination, destination.Length);
            return(new Guid(destination));
        }
예제 #8
0
 internal byte[] HashSHA1FromASCIIToBytes(string plain)
 {
     return(_hashSHA1Generator.Hash(Encoding.ASCII.GetBytes(plain)));
 }
예제 #9
0
 private static string HashUsingUTF16(string bytes, IHashGenerator hashGenerator)
 {
     return(EncodeBase64(hashGenerator.Hash(EncodeUTF16(bytes))));
 }
예제 #10
0
 private static string HashUsingDefaultEncoding(string plain, IHashGenerator hashGenerator)
 {
     return(EncodeBase64(hashGenerator.Hash(EncodeDefault(plain))));
 }
예제 #11
0
 private string Hash(string plain, IHashGenerator hashGenerator)
 {
     return(EncodeBase64(hashGenerator.Hash(Encode(plain))));
 }
예제 #12
0
 private static string Hash(byte[] bytes, IHashGenerator hashGenerator)
 {
     return(EncodeBase64(hashGenerator.Hash(bytes)));
 }
예제 #13
0
 private static byte[] HashToBytes(byte[] bytes, IHashGenerator hashGenerator)
 {
     return(hashGenerator.Hash(bytes));
 }
예제 #14
0
 private static string HexDeriveUsingUTF16(string plainText, string salt, IHashGenerator hashGenerator)
 {
     return((BitConverter.ToString(hashGenerator.Hash(EncodeUTF16(plainText + salt)))).Replace("-", ""));
 }