From: Gunnar Wrobel Date: Wed, 17 Nov 2010 14:02:44 +0000 (+0100) Subject: A first, simple bloom filter. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=a37ef25dcbf3b43bbd634c97a8504f5fb362588b;p=horde.git A first, simple bloom filter. --- diff --git a/framework/Nonce/lib/Horde/Nonce/Filter.php b/framework/Nonce/lib/Horde/Nonce/Filter.php new file mode 100644 index 000000000..2ca4f4f5c --- /dev/null +++ b/framework/Nonce/lib/Horde/Nonce/Filter.php @@ -0,0 +1,51 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Nonce + */ + +/** + * Generates nonces. + * + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Horde + * @package Nonce + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Nonce + */ +class Horde_Nonce_Filter +{ + private $_filter = array(); + + public function isUsed($counter, $hashes) + { + $unused_checks = 0; + foreach ($hashes as $hash) { + if (!isset($this->_filter[$hash]) || $counter > $this->_filter[$hash]) { + $unused_checks++; + } + } + foreach ($hashes as $hash) { + if (!isset($this->_filter[$hash]) || $counter > $this->_filter[$hash]) { + $this->_filter[$hash] = $counter; + } + } + if ($unused_checks > 0) { + return false; + } else { + return true; + } + } +} \ No newline at end of file diff --git a/framework/Nonce/test/Horde/Nonce/StoryTestCase.php b/framework/Nonce/test/Horde/Nonce/StoryTestCase.php index d9ad32091..2fc7e5ace 100644 --- a/framework/Nonce/test/Horde/Nonce/StoryTestCase.php +++ b/framework/Nonce/test/Horde/Nonce/StoryTestCase.php @@ -53,6 +53,12 @@ extends PHPUnit_Extensions_Story_TestCase case 'the default nonce generator': $world['nonce_generator'] = new Horde_Nonce_Generator(); break; + case 'the default filter setup': + $world['nonce_filter'] = new Horde_Nonce_Filter(); + break; + case 'the following counter and hash values are marked': + $world['nonce_filter']->isUsed(array_shift($arguments), $arguments); + break; default: return $this->notImplemented($action); } @@ -85,6 +91,9 @@ extends PHPUnit_Extensions_Story_TestCase list($timestamp, $random) = $world['nonce_generator']->split($arguments[0]); $world['hashes'] = $world['nonce_hash']->hash($random); break; + case 'testing whether a nonce is unused if it has the following counter and hash values': + $world['used'] = $world['nonce_filter']->isUsed(array_shift($arguments), $arguments); + break; default: return $this->notImplemented($action); } @@ -129,6 +138,12 @@ extends PHPUnit_Extensions_Story_TestCase $arguments ); break; + case 'the nonce is unused': + $this->assertFalse($world['used']); + break; + case 'the nonce is used': + $this->assertTrue($world['used']); + break; default: return $this->notImplemented($action); }