add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Random / Source / URandom.php
diff --git a/CryptLib/Random/Source/URandom.php b/CryptLib/Random/Source/URandom.php
new file mode 100644 (file)
index 0000000..5db7056
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+/**
+ * The URandom Random Number Source
+ *
+ * This uses the *nix /dev/urandom device to generate medium strength numbers
+ *
+ * PHP version 5.3
+ *
+ * @category   PHPCryptLib
+ * @package    Random
+ * @subpackage Source
+ * @author     Anthony Ferrara <ircmaxell@ircmaxell.com>
+ * @copyright  2011 The Authors
+ * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
+ * @version    Build @@version@@
+ */
+
+namespace CryptLib\Random\Source;
+
+use CryptLib\Core\Strength;
+
+/**
+ * The URandom Random Number Source
+ *
+ * This uses the *nix /dev/urandom device to generate medium strength numbers
+ *
+ * @category   PHPCryptLib
+ * @package    Random
+ * @subpackage Source
+ * @author     Anthony Ferrara <ircmaxell@ircmaxell.com>
+ * @codeCoverageIgnore
+ */
+class URandom implements \CryptLib\Random\Source {
+
+    /**
+     * @var string The file to read from
+     */
+    protected $file = '/dev/urandom';
+
+    /**
+     * Return an instance of Strength indicating the strength of the source
+     *
+     * @return Strength An instance of one of the strength classes
+     */
+    public static function getStrength() {
+        return new Strength(Strength::MEDIUM);
+    }
+
+    /**
+     * Generate a random string of the specified size
+     *
+     * @param int $size The size of the requested random string
+     *
+     * @return string A string of the requested size
+     */
+    public function generate($size) {
+        if ($size == 0 || !file_exists($this->file)) {
+            return str_repeat(chr(0), $size);
+        }
+        $file = fopen($this->file, 'rb');
+        if (!$file) {
+            return str_repeat(chr(0), $size);
+        }
+        if (function_exists('stream_set_read_buffer')) {
+            stream_set_read_buffer($file, 0);
+        }
+        $result = fread($file, $size);
+        fclose($file);
+        return $result;
+    }
+
+}