/** * {@inheritDoc} * * @return void */ public function run(EntityInterface $user) { $user->setActivationKey(base64_encode($this->secureRandom->nextBytes(24))); $user->setPassword($this->passwordEncoder->encodePassword($user->getPassword(), User::SALT)); $user->setRegistrationDate(new \DateTime()); $this->entityManager->getRepository(User::class)->add($user); $this->entityManager->flush(); }
/** * {@inheritdoc} */ public function generateToken() { // Generate an URI safe base64 encoded string that does not contain "+", // "/" or "=" which need to be URL encoded and make URLs unnecessarily // longer. $bytes = $this->random->nextBytes($this->entropy / 8); return rtrim(strtr(base64_encode($bytes), '+/', '-_'), '='); }
/** * @param int $length * * @return string */ public function generateRandomString($length) { do { $bytes = base64_encode($this->random->nextBytes($length)); $string = substr(strtr($bytes, array('+' => '', '/' => '', '=' => '')), 0, $length); } while (strlen($string) < $length); return $string; }
public function set($note) { if (null === $note->getSecret()) { $note->setSecret(base64_encode($this->randomGenerator->nextBytes(64))); } $this->em->persist($note); $this->em->flush(); }
/** * @param SecureRandomInterface $random From security.secure_random * @param $phase * @param EGGame $game * @param User $player * @return EGPlaySession */ public static function make(SecureRandomInterface $random, $phase, EGGame $game, User $player = null) { $session = new EGPlaySession(); $session->setGame($game); $session->setPlayer($player); $session->setPhase($phase); $session->setStartDate(new \DateTime('now')); $session->setIsCompleted(false); $session->setScore(0); $session->id = bin2hex($random->nextBytes(20)); return $session; }
/** * Generate a new token and persist it into DB * * @param string $scope Purpose of this token (where will it be used) * @param string $ownerType Owner of this entity (could be an Entity class name) * @param integer $ownerId Identifier of the owner * @param integer $usesMax Maximum number of uses for this Token before it is invalidated * @param \DateTime $expiresAt Optional expiry time, after which the Token becomes invalid * @param array $data Optional additional data to check against, for advanced validation conditions * * @return Token */ public function generate($scope, $ownerType, $ownerId, $usesMax = 1, \DateTime $expiresAt = null, array $data = null) { $hash = md5($this->secureRandom->nextBytes(32) . $scope . $ownerType . $ownerId); $token = new Token(); $token->setHash($hash)->setScope($scope)->setOwnerType($ownerType)->setOwnerId($ownerId)->setUsesMax($usesMax); if (!empty($data)) { $token->setData($data); } if ($expiresAt instanceof \DateTime) { $token->setExpiresAt($expiresAt); } $this->em->persist($token); $this->em->flush($token); return $token; }
public function set($note) { if (null === $note->id) { if (empty($this->data)) { $note->id = 0; } else { end($this->data); $note->id = key($this->data) + 1; } } if (null === $note->secret) { $note->secret = base64_encode($this->randomGenerator->nextBytes(64)); } $this->data[$note->id] = $note; $this->flush(); }
public function register(User $user) { $user->initialize(); $user->setUsername($user->getEmail()); $user->setPlainPassword(md5($this->randomGenerator->nextBytes(16))); $user->setUnlockToken(md5($this->randomGenerator->nextBytes(16))); $user->setEnabled(true); $errors = $this->validator->validate($user, ['Registration']); if ($errors->count() === 0) { try { $this->userManager->updateUser($user); } catch (\Exception $e) { throw new Exception($e->getMessage()); } // TODO: send unlock email return true; } return $errors; }
/** * @return bytes 16 random bytes to be used in the salt */ protected function getRawSalt() { $rawSalt = false; $numBytes = 16; if (function_exists('mcrypt_create_iv')) { $rawSalt = mcrypt_create_iv($numBytes, MCRYPT_DEV_URANDOM); } if (!$rawSalt) { $rawSalt = $this->secureRandom->nextBytes($numBytes); } return $rawSalt; }
/** * Tests whether the generated token is eight characters long. */ public function testGeneratedTokenHasLengthOfEight() { $this->random->expects($this->once())->method('nextBytes')->with(self::ENTROPY / 8)->will($this->returnValue(self::$bytes)); $this->assertEquals(8, strlen($this->generator->generateToken())); }
/** * @return string */ private function generateConversationId() { return sha1($this->secureRandom->nextBytes(24)); }