Ejemplo n.º 1
0
 /**
  * Generate a random string of specified length.
  * @param string $length The length of the generated string
  * @param string $characters An optional list of characters to use if no characterlist is
  * 							specified all valid base64 characters except + (plus sign) are used.
  * @return string
  * @throws \Exception If the generator is not initialized.
  */
 public function generate($length, $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./')
 {
     if (is_null($this->generator)) {
         throw new \Exception('Generator is not initialized.');
     }
     return $this->generator->generateString($length, $characters);
 }
Ejemplo n.º 2
0
 /**
  * Generate a random string of specified length.
  * @param string $length The length of the generated string
  * @param string $characters An optional list of characters to use if no characterlist is
  * 							specified 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./
  * 							is used.
  * @return string
  * @throws \Exception If the generator is not initialized.
  */
 public function generate($length, $characters = '')
 {
     if (is_null($this->generator)) {
         throw new \Exception('Generator is not initialized.');
     }
     return $this->generator->generateString($length, $characters);
 }
 function it_generates_a_random_string_of_set_length(Generator $medium, Generator $low)
 {
     $options = ['length' => 8, 'chars' => '', 'strength' => 'low'];
     $this->setOptions($options);
     $medium->generateString(8, '')->shouldNotBeCalled();
     $low->generateString(8, '')->shouldBeCalled();
     $this->generate();
 }
Ejemplo n.º 4
0
 /**
  * Generate a medium-strength random string of the given length.
  *
  * @param int $length length of the generated string
  * @param string $characters characters to use to generate the string
  * @return string
  */
 public function generateString($length, $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
 {
     if (is_null($this->_factory)) {
         $this->_factory = new \RandomLib\Factory();
         $this->_generator = $this->_factory->getMediumStrengthGenerator();
     }
     return $this->_generator->generateString($length, $characters);
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function write($user, $remember = false)
 {
     if ($token = $this->getToken()) {
         $this->connection->delete($this->config['table'], ['id' => sha1($token)]);
     }
     $id = $this->random->generateString(64);
     $this->cookie->set($this->config['cookie']['name'], $id, $this->config['cookie']['lifetime'] + time());
     $this->createTable();
     $this->connection->insert($this->config['table'], ['id' => sha1($id), 'user_id' => $user, 'access' => date('Y-m-d H:i:s'), 'status' => $remember ? self::STATUS_REMEMBERED : self::STATUS_ACTIVE, 'data' => json_encode(['ip' => $this->getRequest()->getClientIp(), 'user-agent' => $this->getRequest()->headers->get('User-Agent')])]);
 }
Ejemplo n.º 6
0
 public function getSecretForUser(User $user)
 {
     $userKey = '_' . (string) $user->getId();
     if (isset($this->secrets[$userKey])) {
         return $this->secrets[$userKey];
     }
     if (null === ($secret = $this->repository->findOneBy(['creator' => $user], ['created' => 'DESC']))) {
         $token = $this->generator->generateString(64, Generator::CHAR_ALNUM | Generator::CHAR_SYMBOLS);
         $secret = new Secret($user, $token);
         $this->repository->save($secret);
     }
     $this->secrets[$userKey] = $secret;
     return $secret;
 }
 /**
  * Generates a password of the given length and complexity.
  *
  * The length should be an integer (and for secure password at least of length 20)
  *
  * @param integer $length     Number of characters in the password. Defaults to 20.
  * @param integer $complexity Complexity of the resulting password. Defaults to
  *                            `PasswordPlease::COMPLEXITY_VERY_HIGH`.
  *
  * @return string              Password of the given length and complexity.
  */
 public function generatePassword($length = self::DEFAULT_LENGTH, $complexity = self::DEFAULT_COMPLEXITY)
 {
     $complexity = $this->getComplexityByName($complexity);
     if (!isset($this->characters[$complexity])) {
         throw new \InvalidArgumentException(sprintf('The given complexity "%s" does not exist. The following complexities are currently supported: %s', $complexity, implode(', ', array_keys($this->characters))));
     }
     if ($length < 1) {
         throw new \InvalidArgumentException('Would you be so kind to provide a password length greater than 0.');
     }
     $characters = '';
     for ($i = $complexity; $i <= self::COMPLEXITY_LOW; $i++) {
         $characters .= $this->characters[$i];
     }
     return $this->generator->generateString($length, $characters);
 }
 /**
  * Save a authorisation code string to the session.
  *
  * @throws Exception\SystemSetupException
  *
  * @return string
  */
 public function setCodeToken()
 {
     $codeValue = $this->random->generateString(32);
     $this->session->set(self::TOKEN_CODE, $codeValue);
     // Retrive the saved token to make sure that the Session is working properly
     $codeValue = $this->getToken(self::TOKEN_CODE);
     if (empty($codeValue)) {
         throw new Exception\SystemSetupException('Unable to create a Symfony session token!');
     }
     return $codeValue;
 }
Ejemplo n.º 9
0
 protected function doTestGenerate(\RandomLib\Generator $generator, $times)
 {
     $inside = 0;
     $outside = 0;
     $on = 0;
     for ($i = 0; $i < $times; $i++) {
         $byte = $generator->generate(2);
         $byte = unpack('n', $byte);
         $byte = array_shift($byte);
         $xCoord = $byte >> 8;
         $yCoord = $byte & 0xff;
         if ($xCoord < $yCoord) {
             $outside++;
         } elseif ($xCoord == $yCoord) {
             $on++;
         } else {
             $inside++;
         }
     }
     $this->assertGreaterThan(0, $outside, 'Outside Is 0');
     $this->assertGreaterThan(0, $inside, 'Inside Is 0');
     $ratio = $inside / $outside;
     return $ratio;
 }
Ejemplo n.º 10
0
 /**
  * Generate a random string of specified length.
  * @param int $length The length of the generated string
  * @param string $characters An optional list of characters to use if no character list is
  * 							specified all valid base64 characters are used.
  * @return string
  * @throws \Exception If the generator is not initialized.
  */
 public function generate($length, $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')
 {
     if (is_null($this->generator)) {
         throw new \Exception('Generator is not initialized.');
     }
     if (function_exists('random_int')) {
         $maxCharIndex = strlen($characters) - 1;
         $randomString = '';
         while ($length > 0) {
             $randomNumber = random_int(0, $maxCharIndex);
             $randomString .= $characters[$randomNumber];
             $length--;
         }
         return $randomString;
     }
     return $this->generator->generateString($length, $characters);
 }
Ejemplo n.º 11
0
 /**
  * Generates a random hexadecimal string for SPChallenge parameter of DigiDoc
  *
  * The generated string is cryptographically secure.
  *
  * @return string
  */
 private function generateChallenge()
 {
     return $this->random->generateString(self::SP_CHALLENGE_LENGTH, '0123456789ABCDEF');
 }
Ejemplo n.º 12
0
 /**
  * Sets the password for a user.
  *
  * @param user   $user
  * @param string $password
  */
 private function doSetPassword(User $user, $password)
 {
     $user->setNonce($this->generator->generateString(64));
     $user->setPassword($this->passwordEncoder->encodePassword($password, $user->getNonce()));
     $user->setSaltedPassword(true);
 }
Ejemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function generateId()
 {
     return $this->generator->generateString($this->length, $this->characters);
 }
Ejemplo n.º 14
0
 /**
  * Generates a string of random binary data of the specified length
  *
  * @param integer $length The number of bytes of random binary data to generate
  * @return string A binary string
  */
 public function generate($length)
 {
     return $this->generator->generate($length);
 }
Ejemplo n.º 15
0
 /**
  * Subscribes to an specific event given a subject and a queue.
  *
  * @param string   $subject  Message topic.
  * @param string   $queue    Queue name.
  * @param \Closure $callback Closure to be executed as callback.
  *
  * @return string
  */
 public function queueSubscribe($subject, $queue, \Closure $callback)
 {
     $sid = $this->randomGenerator->generateString(16);
     $msg = 'SUB ' . $subject . ' ' . $queue . ' ' . $sid;
     $this->send($msg);
     $this->subscriptions[$sid] = $callback;
     return $sid;
 }