コード例 #1
0
        /// <summary>
        /// Validates the security of a password based on the number
        /// of times it has been breached according to Pwned Passwords.
        /// </summary>
        /// <param name="password">A user password</param>
        /// <returns></returns>
        public PasswordStatus Validate(string password)
        {
            // Use the hash function to get the hash value of the password.
            _hashValue = _hashFunction.GetHashValue(password).Hash + _hashFunction.GetHashValue(password).Salt;
            Console.WriteLine("Hash Value: " + _hashValue); // Demo

            // First 5 characters of the hash value
            string prefix = _hashValue.Substring(0, 5);

            // Full URL address
            Uri uri = new Uri(_url + prefix);

            Console.WriteLine("Url: " + uri); // Demo

            // Http GET request
            Task <string> response       = client.RequestData(uri);
            string        hashlistString = response.Result;

            // Deserialize json into a dictionary<hashValue,counts>
            Dictionary <string, int> hashes = JsonToDictionary(hashlistString);

            // Find the hashvalue in the dictionary
            int hashCount = FindHash(_hashValue.Substring(5), hashes);

            // Check business rules
            PasswordStatus status = PasswordCheckingBR.CheckPasswordCount(hashCount);

            return(status);
        }