Example #1
0
        internal static string ComputeHash(string value)
        {
            if (s_useEncryption)
            {
                EnsureConfig();
                HashAlgorithm algorithm = (HashAlgorithm)s_oHashAlgoStack.Pop();
                if (null == algorithm)
                {
                    algorithm = NewHashAlgorithm();
                }

                byte[]   encrypted;
                byte[]   bytes  = new byte[ADP.CharSize * value.Length];
                GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
                try {
                    System.Text.Encoding.Unicode.GetBytes(value, 0, value.Length, bytes, 0);
                    encrypted = algorithm.ComputeHash(bytes);
                }
                finally {
                    Array.Clear(bytes, 0, bytes.Length);
                    if (handle.IsAllocated)
                    {
                        handle.Free();
                    }
                }
                s_oHashAlgoStack.Push(algorithm);
                return(System.Text.Encoding.Unicode.GetString(encrypted, 0, encrypted.Length));
            }
            return(value);
        }
Example #2
0
        private static ICryptoTransform GetCryptoTransform(bool fEncrypt)
        {
            InterlockedStack st = (fEncrypt ? s_oEncryptorStack : s_oDecryptorStack);
            ICryptoTransform ct = (st.Pop() as ICryptoTransform);

            if (null == ct)
            {
                ct = NewCryptTransform(fEncrypt);
            }
            return(ct);
        }
Example #3
0
        private void CleanupCallback(Object state)
        {
            // Called when the cleanup-timer ticks over.
            //
            // This is the automatic prunning method.  Every period, we will perform a two-step
            // process.  First, for the objects above MinPool, we will obtain the semaphore for
            // the object and then destroy it if it was on the old stack.  We will continue this
            // until we either reach MinPool size, or we are unable to obtain a free object, or
            // until we have exhausted all the objects on the old stack.  After that, push all
            // objects on the new stack to the old stack.  So, every period the objects on the
            // old stack are destroyed and the objects on the new stack are pushed to the old
            // stack.  All objects that are currently out and in use are not on either stack.
            // With this logic, a object is prunned if unused for at least one period but not
            // more than two periods.

            // Destroy free objects above MinPool size from old stack.
#if USECOUNTEROBJECT
            while (_poolCounter.TotalCount > _ctrl.MinPool)
#else //!USECOUNTEROBJECT
            while (Count > _ctrl.MinPool)
#endif //!USECOUNTEROBJECT
            {
                // While above MinPoolSize...

                if (_waitHandles[SEMAPHORE_HANDLE].WaitOne(0, false) /* != WAIT_TIMEOUT */)
                {
                    // We obtained a objects from the semaphore.
                    DBPooledObject obj = (DBPooledObject)_stackOld.Pop();

                    if (null != obj)
                    {
                        // If we obtained one from the old stack, destroy it.
#if USECOUNTEROBJECT
                        _poolCounter.Modify(-cAddFree);
#endif //USECOUNTEROBJECT
                        DestroyObject(obj);
                    }
                    else
                    {
                        // Else we exhausted the old stack, so break.
                        SafeNativeMethods.ReleaseSemaphore(_waitHandles[SEMAPHORE_HANDLE].Handle, 1, IntPtr.Zero);
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            // Push to the old-stack.  For each free object, move object from new stack
            // to old stack.
            if (_waitHandles[SEMAPHORE_HANDLE].WaitOne(0, false) /* != WAIT_TIMEOUT */)
            {
                for (;;)
                {
                    DBPooledObject obj = (DBPooledObject)_stackNew.Pop();

                    if (null == obj)
                    {
                        break;
                    }

                    _stackOld.Push(obj);
                }
                SafeNativeMethods.ReleaseSemaphore(_waitHandles[SEMAPHORE_HANDLE].Handle, 1, IntPtr.Zero);
            }

            // Make sure we're at quota by posting a callback to the threadpool.
            ThreadPool.QueueUserWorkItem(new WaitCallback(PoolCreateRequest));
        }