diff --git a/src/applications/people/storage/user/PhabricatorUser.php b/src/applications/people/storage/user/PhabricatorUser.php index eae33c1ccf..ab5a3f9e7a 100644 --- a/src/applications/people/storage/user/PhabricatorUser.php +++ b/src/applications/people/storage/user/PhabricatorUser.php @@ -68,26 +68,12 @@ class PhabricatorUser extends PhabricatorUserDAO { } private function generateConduitCertificate() { - $entropy = $this->generateEntropy($bytes = 256); + $entropy = Filesystem::readRandomBytes(256); $entropy = base64_encode($entropy); $entropy = substr($entropy, 0, 255); return $entropy; } - private function generateEntropy($bytes) { - $urandom = fopen('/dev/urandom', 'r'); - if (!$urandom) { - throw new Exception("Failed to open /dev/urandom!"); - } - - $entropy = fread($urandom, $bytes); - if (strlen($entropy) != $bytes) { - throw new Exception("Failed to read /dev/urandom!"); - } - - return $entropy; - } - public function comparePassword($password) { $password = $this->hashPassword($password); return ($password === $this->getPasswordHash()); @@ -137,7 +123,7 @@ class PhabricatorUser extends PhabricatorUserDAO { public function establishSession($session_type) { $conn_w = $this->establishConnection('w'); - $entropy = $this->generateEntropy($bytes = 20); + $entropy = Filesystem::readRandomBytes(20); $session_key = sha1($entropy); queryfx( diff --git a/src/applications/people/storage/user/__init__.php b/src/applications/people/storage/user/__init__.php index 09bebc3ee3..3be82cddf2 100644 --- a/src/applications/people/storage/user/__init__.php +++ b/src/applications/people/storage/user/__init__.php @@ -11,6 +11,7 @@ phutil_require_module('phabricator', 'applications/phid/storage/phid'); phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'storage/queryfx'); +phutil_require_module('phutil', 'filesystem'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/phid/storage/phid/PhabricatorPHID.php b/src/applications/phid/storage/phid/PhabricatorPHID.php index 6289311f6a..08053a0d1c 100644 --- a/src/applications/phid/storage/phid/PhabricatorPHID.php +++ b/src/applications/phid/storage/phid/PhabricatorPHID.php @@ -31,14 +31,7 @@ class PhabricatorPHID extends PhabricatorPHIDDAO { throw new Exception("Can not generate PHID with no type."); } - $urandom = @fopen('/dev/urandom', 'r'); - if (!$urandom) { - throw new Exception("Failed to open /dev/urandom!"); - } - $entropy = fread($urandom, 20); - if (strlen($entropy) != 20) { - throw new Exception("Failed to read from /dev/urandom!"); - } + $entropy = Filesystem::readRandomBytes(20); $uniq = sha1($entropy); $uniq = substr($uniq, 0, 20); diff --git a/src/applications/phid/storage/phid/__init__.php b/src/applications/phid/storage/phid/__init__.php index 5ff7852286..11b016fe93 100644 --- a/src/applications/phid/storage/phid/__init__.php +++ b/src/applications/phid/storage/phid/__init__.php @@ -8,6 +8,7 @@ phutil_require_module('phabricator', 'applications/phid/storage/base'); +phutil_require_module('phutil', 'filesystem'); phutil_require_module('phutil', 'utils');