A first, simple bloom filter.
authorGunnar Wrobel <p@rdus.de>
Wed, 17 Nov 2010 14:02:44 +0000 (15:02 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 30 Nov 2010 12:48:24 +0000 (13:48 +0100)
framework/Nonce/lib/Horde/Nonce/Filter.php [new file with mode: 0644]
framework/Nonce/test/Horde/Nonce/StoryTestCase.php

diff --git a/framework/Nonce/lib/Horde/Nonce/Filter.php b/framework/Nonce/lib/Horde/Nonce/Filter.php
new file mode 100644 (file)
index 0000000..2ca4f4f
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Generates nonces.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Nonce
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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
index d9ad320..2fc7e5a 100644 (file)
@@ -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);
         }