Exemplo n.º 1
0
        /// <summary>
        /// Creates new Cache Digest value from urls collection.
        /// </summary>
        /// <param name="urls">The collection of URLs with ETag. URLs should be converted to an ASCII string by percent-encoding as appropriate per RFC3986 and ETags should include both the weak indicator (if present) and double quotes.</param>
        /// <param name="probability">The probability of hashes collision.</param>
        /// <param name="validators">The value indicating that the validators should be included in the digest.</param>
        /// <returns>Cache Digest value.</returns>
        public static CacheDigestValue FromUrls(IDictionary <string, string> urls, uint probability = DefaultProbability, bool validators = false)
        {
            // Let N be the count of URLs’ members, rounded to the nearest power of 2 smaller than 2**32.
            int count = CalculateCount(urls?.Count ?? 0);

            try
            {
                ValidateProbability(probability);
            }
            catch (FormatException)
            {
                probability = DefaultProbability;
            }

            // Let hash-values be an empty array of integers.
            HashSet <uint> hashes = new HashSet <uint>();

            if (urls != null)
            {
                // For each (URL, ETag) in URLs, compute a hash value (Section 2.1.2) and append the result to hash-values.
                foreach (KeyValuePair <string, string> url in urls)
                {
                    hashes.Add(CacheDigestHashAlgorithm.ComputeHash(url.Key, count, probability, validators, url.Value));
                }
            }

            return(new CacheDigestValue
            {
                Count = count,
                Probability = probability,
                Hashes = hashes
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if digest value contains given URL.
        /// </summary>
        /// <param name="url">The URL, converted to an ASCII string by percent-encoding as appropriate per RFC3986.</param>
        /// <param name="validators">Flag indicating if digest value is expected to contain validators.</param>
        /// <param name="entityTag">The ETag, including both the weak indicator (if present) and double quotes, as per RFC7232 Section 2.3.</param>
        /// <returns>True if there is a match in the digest value, otherwise false.</returns>
        public bool Contains(string url, bool validators = false, string entityTag = null)
        {
            uint hash = CacheDigestHashAlgorithm.ComputeHash(url, Count, Probability, validators, entityTag);

            return(Contains(hash));
        }