diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index a9c7bfc031a18570cd0f885a3a5b314f83a057df..2a15674e2179213be41501b8b8cf16c8386d488d 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -1006,6 +1006,10 @@ class SimpleSAML_Utilities {
 		static $fp = NULL;
 		assert('is_int($length)');
 
+		if (function_exists('openssl_random_pseudo_bytes')) {
+			return openssl_random_pseudo_bytes($length);
+		}
+
 		if($fp === NULL) {
 			if (@file_exists('/dev/urandom')) {
 				$fp = @fopen('/dev/urandom', 'rb');