コード例 #1
0
        public async Task <AuthenticatedUser> Authenticate(LoginCredentials credentials)
        {
            var userID = await _repository.GetUsersIdAsync(credentials.Email);

            if (userID == null)
            {
                return(null);
            }

            byte[] salt = userID.ToByteArray();
            //user id is salt for hash function
            string hashedPassword = _hash.Hash(salt, credentials.Password);

            var user = await _repository.GetUserAsync(credentials.Email, hashedPassword);

            if (user == null)
            {
                return(null);
            }

            return(new AuthenticatedUser()
            {
                Id = user.Id, Name = user.Name, Email = user.Email, Token = CreateToken(user)
            });
        }
コード例 #2
0
        public async Task <AuthenticatedUser> Authenticate(LoginCredentials credentials)
        {
            var userID = await _repository.GetUsersIdAsync(credentials.Login);

            if (userID == null)
            {
                return(null);
            }

            byte[] salt = userID.ToByteArray();
            //солью для хэширования пароля является id пользователя
            string hashedPassword = _hash.Hash(salt, credentials.Password);

            var user = await _repository.GetUserAsync(credentials.Login, hashedPassword);

            if (user == null)
            {
                return(null);
            }

            return(new AuthenticatedUser()
            {
                Id = user.Id, Login = user.Login, Token = CreateToken(user)
            });
        }
コード例 #3
0
        /// <summary>
        ///  indicating whether the key was found
        /// </summary>
        /// <param name="key"> giving the key to look for</param>
        /// <param name="value">store the value associated with the given key</param>
        /// <returns></returns>
        public bool TryGetValue(string key, out T value)
        {
            if (key == null)
            {
                throw new ArgumentException();
            }

            int firstLoc = _primaryHashFunction.Hash(key);

            value = default(T);
            if (_primaryTable[firstLoc] == null)
            {
                return(false);
            }
            else
            {
                firstLoc = _primaryTable[firstLoc].Hash(key) + _offsets[firstLoc];
                if (key.Equals(_secondaryTables[firstLoc].Key))
                {
                    value = _secondaryTables[firstLoc].Value;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
コード例 #4
0
        private int GetHash(object obj)
        {
            int output;

            using (var stream = new MemoryStream(ObjectToStream(obj)))
            {
                output = _hash.Hash(stream);
            }

            return(output);
        }
コード例 #5
0
        /// <summary>
        /// method to find the primary hash function
        /// </summary>
        /// <param name="list"> containing all of the keys and values.</param>
        /// <param name="hashTable">giving a hash table to fill</param>
        /// <param name="maxKeyLength"> giving the maximum length of any key</param>
        /// <returns> giving the sum of the squares of the sizes of each list in the hash table that was filled.</returns>
        private int FindPrimaryHashFunction(IList <KeyValuePair <string, T> > list, IList <KeyValuePair <string, T> >[] hashTable, int maxKeyLength)
        {
            long sum = 0;

            do
            {
                _primaryHashFunction = new RandomHashFunction(list.Count, maxKeyLength);
                for (int i = 0; i < hashTable.Length; i++)
                {
                    hashTable[i] = new List <KeyValuePair <string, T> >();
                }

                foreach (KeyValuePair <string, T> keyPair in list)
                {
                    hashTable[_primaryHashFunction.Hash(keyPair.Key)].Add(keyPair);
                }
                sum = SumOfSquares(hashTable);
            } while ((double)sum > (list.Count * _maximumSecondaryLengthFactor));
            return((int)sum);
        }
コード例 #6
0
        /// <summary>
        /// method to determine if a given hash function mapping is perfect
        /// </summary>
        /// <param name="secondHashfunction">giving the secondary hash function to test.</param>
        /// <param name="firstTargetLoc">giving the first array location of the target secondary hash table</param>
        /// <param name="secondHashTableLength">giving the length of the target secondary hash table</param>
        /// <param name="elements">elements to place in the target secondary hash table.</param>
        /// <returns>indicating whether the mapping is perfect </returns>
        private bool IsPerfect(IHashFunction secondHashfunction, int firstTargetLoc, int secondHashTableLength, IList <KeyValuePair <string, T> > elements)
        {
            int loc;

            foreach (KeyValuePair <string, T> keyPair in elements)
            {
                loc = secondHashfunction.Hash(keyPair.Key) + firstTargetLoc;
                if (_secondaryTables[loc].Key == null)
                {
                    _secondaryTables[loc] = keyPair;
                }
                else
                {
                    for (int i = firstTargetLoc; i < firstTargetLoc + secondHashTableLength; i++)
                    {
                        _secondaryTables[i] = default(KeyValuePair <string, T>);
                    }
                    return(false);
                }
            }
            return(true);
        }
コード例 #7
0
 private int GetCorrectHash(string value) => Math.Abs(hashFunction.Hash(value)) % size;
コード例 #8
0
ファイル: UsersService.cs プロジェクト: zlre/ParrotWings
        public async Task <bool> AddUserAsync(RegisterCredentials credentials)
        {
            using (var context = _contextFactory.Invoke())
            {
                Guid id = await GetUsersIdAsync(credentials.Email);

                if (id != default)
                {
                    return(false);
                }

                id = Guid.NewGuid();
                //user id is salt for hash function
                var user = new User()
                {
                    Id = id, Name = credentials.Name, Email = credentials.Email, Password = _hash.Hash(id.ToByteArray(), credentials.Password)
                };
                context.Users.Add(user);
                await context.SaveChangesAsync();
            }

            return(true);
        }
コード例 #9
0
 private static int CalculateIndex(string value, IHashFunction hashFunction, int hashTableSize)
 {
     // Must always return positive.
     return((hashFunction.Hash(value.ToString()) % hashTableSize) & 0x7FFFFFFF);
 }