示例#1
0
        /// <summary>
        /// Removes the specified data from the counting bloom filter.
        /// </summary>
        /// <param name="data">The data to remove from this counting bloom filter.</param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if data is null.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrownf if data is empty.
        /// </exception>
        public void Remove(byte[] data)
        {
            #region Input validation

            Insist.IsNotNull(data, "data");
            Insist.IsNotEmpty(data, "data");

            #endregion

            int[] computedIndeces = GetIndeces(data);

            _lock.EnterWriteLock();
            try
            {
                foreach (int index in computedIndeces)
                {
                    if (_bucketStorage[index] > 0)
                    {
                        _bucketStorage[index]--;
                    }
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
示例#2
0
        /// <summary>
        /// Checks to see if the supplied data has been added to this filter.
        /// </summary>
        /// <param name="data">
        /// The data to check for presence in the filter.
        /// </param>
        /// <returns>
        /// True if the item *might* have been added to the filter. False if it
        /// definitely has not.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if data is null.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrownf if data is empty.
        /// </exception>
        public bool IsPresent(byte[] data)
        {
            #region Input validation

            Insist.IsNotNull(data, "data");
            Insist.IsNotEmpty(data, "data");

            #endregion

            int[] computedIndeces = GetIndeces(data);

            _lock.EnterReadLock();
            try
            {
                foreach (int index in computedIndeces)
                {
                    if (IsBucketInUse(index) == false)
                    {
                        return(false);
                    }
                }
            }
            finally
            {
                _lock.ExitReadLock();
            }

            return(true);
        }
示例#3
0
        public void IsNotEmpty_2_Null_Or_Empty_User_Message_Uses_Default_Message(string userMessage)
        {
            string message = null;

            try {
                Insist.IsNotEmpty(new string[0], ARGUMENT_NAME, userMessage);
            } catch (ArgumentException e) {
                message = e.Message;
            }

            Assert.IsNotNullOrEmpty(message);
        }
        /// <summary>
        /// Generates a new one time password from the supplied data.
        /// </summary>
        /// <param name="secretKey">The secret key to use in the HMAC</param>
        /// <param name="hmac">The hmac algorithm to use</param>
        /// <param name="dt">The date and time to generate a code for</param>
        /// <param name="offset">Any offsets that should be applied to the supplie date time</param>
        /// <param name="timeStep">The timestep value to use to calculate the current step</param>
        /// <param name="otpLength">The required legnth of the returned passcode</param>
        /// <returns>A one time password code</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if hmac or secret key is null</exception>
        /// <exception cref="System.ArgumentException">Thrown if secret key is empty, optLength is
        /// not defined value or timeStep is less than 1 second.</exception>
        public static string Generate(byte[] secretKey, HMAC hmac, DateTime dt, TimeSpan offset, TimeSpan timeStep, OneTimePasswordLength otpLength)
        {
            #region Input validation

            Insist.IsNotNull(hmac, "hmac");
            Insist.IsNotNull(secretKey, "secretKey");
            Insist.IsNotEmpty(secretKey, "secretKey");
            Insist.IsDefined <OneTimePasswordLength>(otpLength, "optLength");
            Insist.IsAtLeast(timeStep.TotalSeconds, 1, "timeStep");

            #endregion

            dt = dt + offset;

            ulong stepNumber = (ulong)Math.Floor((double)dt.ToUnixTime() / (double)timeStep.TotalSeconds);

            return(HmacOneTimePassword.Generate(secretKey, stepNumber, hmac, otpLength));
        }
示例#5
0
        /// <summary>
        /// Adds the specified data to the bloom filter.
        /// </summary>
        /// <param name="data">The data to track in this bloom filter.</param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if data is null.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrownf if data is empty.
        /// </exception>
        public void Add(byte[] data)
        {
            #region Input validation

            Insist.IsNotNull(data, "data");
            Insist.IsNotEmpty(data, "data");

            #endregion

            int[] computedIndeces = GetIndeces(data);

            _lock.EnterWriteLock();
            try
            {
                bool itemAlreadyPresent = true;
                foreach (int index in computedIndeces)
                {
                    if (IsBucketInUse(index) == false)
                    {
                        itemAlreadyPresent = false;
                        break;
                    }
                }

                if (itemAlreadyPresent == false)
                {
                    foreach (int index in computedIndeces)
                    {
                        SetBucketAsInUse(index);
                    }
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
示例#6
0
 public void IsNotEmpty_2_Non_Empty_Collection_Does_Not_Throw_Exception()
 {
     Assert.DoesNotThrow(() => { Insist.IsNotEmpty(new string[] { "Hello, World" }, ARGUMENT_NAME, MESSAGE); });
 }
示例#7
0
 public void IsNotEmpty_2_Empty_Collection_Throws_Exception()
 {
     Insist.IsNotEmpty(new string[0], ARGUMENT_NAME, MESSAGE);
 }
示例#8
0
 public void IsNotEmpty_2_Null_Or_Empty_Argument_Name_Throws_Exception(string argName)
 {
     Insist.IsNotEmpty(new string[] { "Hello, World" }, argName, MESSAGE);
 }