本文整理汇总了PHP中Zend\Crypt\Password\Bcrypt类的典型用法代码示例。如果您正苦于以下问题:PHP Bcrypt类的具体用法?PHP Bcrypt怎么用?PHP Bcrypt使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bcrypt类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save
public function save($entity)
{
if (!isset($entity->zfcuser) || !$entity->zfcuser instanceof UserInterface) {
throw new \RuntimeException('Entity must implement ZfcUser\\Entity\\UserInterface');
}
// If the user specified a new password, hash it
$password = $entity->zfcuser->getPassword();
if (!empty($password)) {
$hydrator = $this->getFieldset()->getHydrator();
if (method_exists($hydrator, 'getCryptoService')) {
// ZfcUser dev-master
$hash = $this->getFieldset()->getHydrator()->getCryptoService()->create($password);
} else {
$bcrypt = new Bcrypt();
$bcrypt->setCost($this->getUserService()->getOptions()->getPasswordCost());
$hash = $bcrypt->create($password);
}
$entity->zfcuser->setPassword($hash);
// Clear out the password values now that we don't need them again
$this->getFieldset()->get('password')->setValue('');
$this->getFieldset()->get('passwordVerify')->setValue('');
}
// Reload the actual user entity and transfer changes to it
// (necessary for ZfcUserDoctrineORM to work, as $entity->zfcuser is disconnected)
$userobj = $this->getUserService()->getUserMapper()->findById($entity->zfcuser->getId());
$this->transferChangesToExistingEntity($entity->zfcuser, $userobj);
// Stash the new entity back in the original's place so that later
// extensions can use it in Doctrine associations safely
$entity->zfcuser = $userobj;
return $this->getUserService()->getUserMapper()->update($userobj);
}
示例2: registerAction
public function registerAction()
{
$request = $this->getRequest();
$form = new UserForm();
$userNameConflict = false;
if ($request->isPost()) {
// check if the form is valid
$form->setData($request->getPost());
$form->setInputFilter(new UserInputFilter());
if ($form->isValid()) {
$data = $form->getData();
$userRepo = $this->getObjectManager()->getRepository(User::class);
$userNameConflict = $userRepo->findOneBy(['userName' => $data['username']]) instanceof User;
if ($userNameConflict) {
$form->get('username')->setValue('');
} else {
// if the requested username is not taken yet, create the password and redirect the user to the login
$user = new User();
$user->setEmail($data['email']);
$user->setUserName($data['username']);
$bcrypt = new Bcrypt();
$password = $bcrypt->create($data['password']);
$user->setPassword($password);
$this->getObjectManager()->persist($user);
$this->getObjectManager()->flush();
return $this->redirect()->toRoute('application/user', ['action' => 'login']);
}
}
}
return new ViewModel(['form' => $form, 'userNameConflict' => $userNameConflict]);
}
示例3: hashPassword
/**
* Retorna hash Bcrypt del password del usuario
*/
public static function hashPassword($password, $cost)
{
$bcrypt = new Bcrypt();
$bcrypt->setCost($cost);
$securePass = $bcrypt->create($password);
return $securePass;
}
示例4: create
/**
* This method inspects the request and routes the data
* to the correct method
*
* @return void
*/
public function create($unfilteredData)
{
$usersTable = $this->getUsersTable();
$filters = $usersTable->getInputFilter();
$filters->setData($unfilteredData);
if ($filters->isValid()) {
$data = $filters->getValues();
$avatarContent = array_key_exists('avatar', $unfilteredData) ? $unfilteredData['avatar'] : NULL;
$bcrypt = new Bcrypt();
$data['password'] = $bcrypt->create($data['password']);
if ($usersTable->create($data)) {
$user = $usersTable->getByUsername($data['username']);
if (!empty($avatarContent)) {
$userImagesTable = $this->getUserImagesTable();
$filename = sprintf('public/images/%s.png', sha1(uniqid(time(), TRUE)));
$content = base64_decode($avatarContent);
$image = imagecreatefromstring($content);
if (imagepng($image, $filename) === TRUE) {
$userImagesTable->create($user['id'], basename($filename));
}
imagedestroy($image);
$image = $userImagesTable->getByFilename(basename($filename));
$usersTable->updateAvatar($image['id'], $user['id']);
}
Mailer::sendWelcomeEmail($user['email'], $user['name']);
$result = new JsonModel(array('result' => true));
} else {
$result = new JsonModel(array('result' => false));
}
} else {
$result = new JsonModel(array('result' => false, 'errors' => $filters->getMessages()));
}
return $result;
}
示例5: load
public function load(ObjectManager $manager)
{
$userFlop = false;
$bcrypt = new Bcrypt();
$bcrypt->setCost(14);
$scope1 = new OAuth2Scope();
$scope1->setScope('read');
$scope1->setIsDefault(true);
$manager->persist($scope1);
$scope2 = new OAuth2Scope();
$scope2->setScope('update');
$scope2->setIsDefault(false);
$manager->persist($scope2);
$scope3 = new OAuth2Scope();
$scope3->setScope('delete');
$scope3->setIsDefault(false);
$manager->persist($scope3);
$scope4 = new OAuth2Scope();
$scope4->setScope('create');
$scope4->setIsDefault(false);
$manager->persist($scope4);
$user2 = new Entity\User();
$user2->setUsername('user2');
$user2->setPassword($bcrypt->create('user2password'));
$user2->setEmail('tom.h.anderson@gmail.com');
$user2->setDisplayName('Tom Anderson');
$manager->persist($user2);
$client2 = new OAuth2Client();
$client2->setClientId('readonly');
$client2->setSecret($bcrypt->create('readonly_password'));
$client2->setGrantType(array('client_credentials', 'refresh_token'));
$client2->setUser($user2);
$client2->addScope($scope1);
$scope1->addClient($client2);
$manager->persist($client2);
// Artists
$artist = new Entity\Artist();
$artist->setName('Grateful Dead');
$manager->persist($artist);
$albums = array('The Grateful Dead', 'Anthem of the Sun', 'Aoxomoxoa', 'Live/Dead', 'Workingman\'s Dead', 'American Beauty');
foreach ($albums as $name) {
$album = new Entity\Album();
$album->setArtist($artist);
$album->setName($name);
$manager->persist($album);
$userAlbum = new Entity\UserAlbum();
$userAlbum->setAlbum($album);
if ($userFlop = !$userFlop) {
# $userAlbum->setUser($user1);
} else {
$userAlbum->setUser($user2);
}
$userAlbum->setDescription("Description for {$name}");
$manager->persist($userAlbum);
}
$loop = new Entity\TestLoop();
$loop->setParentLoop($loop);
$manager->persist($loop);
$manager->flush();
}
示例6: authenticate
/**
* Performs an authentication attempt
*
* @return \Zend\Authentication\Result
* @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface If authentication cannot be performed
*/
public function authenticate()
{
if (empty($this->_username) || empty($this->_password)) {
// throw new \Zend\Authentication\Adapter\Exception();
}
if (is_null($this->_dm)) {
throw new \Exception('Document Manager is null');
}
$query = $this->_dm->createQueryBuilder('MoveIn4User\\Document\\UserDocument');
$query->field('userName')->equals((string) $this->_username);
// $query->field ( 'password' )->equals ((string) $this->_password );
$query->field('active')->equals(true);
$query->field('deleted')->equals(false);
$users = $query->getQuery()->execute();
if (count($users) === 0) {
return new Result(Result::FAILURE_CREDENTIAL_INVALID, array());
} else {
foreach ($users as $user) {
$bcrypt = new Bcrypt();
if ($bcrypt->verify((string) $this->_password, $user->getPassword()) and $user->getInstance()->getDeleted() === false) {
return new Result(Result::SUCCESS, array('username' => $user->getUserName(), 'firstName' => $user->getFirstName(), 'surname' => $user->getSurname(), 'defaultLanguage' => $user->getInstance()->getDefaultLanguage(), 'id' => $user->getId()));
}
return new Result(Result::FAILURE_CREDENTIAL_INVALID, array());
}
}
}
示例7: authenticate
public function authenticate($username, $password)
{
$callback = function ($password, $hash) {
$bcrypt = new Bcrypt();
return $bcrypt->verify($hash, $password);
};
$authenticationService = new AuthenticationService();
$callbackCheckAdapter = new CallbackCheckAdapter($this->dbAdapter, "users", 'username', 'password', $callback);
$callbackCheckAdapter->setIdentity($username)->setCredential($password);
$authenticationService->setAdapter($callbackCheckAdapter);
$authResult = $authenticationService->authenticate();
if ($authResult->isValid()) {
$userObject = $callbackCheckAdapter->getResultRowObject();
$authenticationService->getStorage()->write($userObject);
if ($userObject->status == 0) {
$authenticationService->clearIdentity();
$this->setCode(-5);
return false;
} else {
return true;
}
} else {
$this->setCode($authResult->getCode());
return false;
}
}
示例8: indexAction
public function indexAction()
{
if (@$_SESSION['user']['Logged'] == 1 || @$_SESSION['user']['Level'] == 1) {
return $this->redirect()->toRoute('application');
}
$userContainer = new Container('user');
$userContainer->Logged;
$userContainer->Level;
$userContainer->Nom;
$userContainer->Prenom;
$form = new AuthForm();
$request = $this->getRequest();
if ($request->isPost()) {
$bcrypt = new Bcrypt();
$User = new User();
$User->user_login = $request->getPost('login');
$User->user_password = $request->getPost('motdepasse');
$UserO = $this->getUserTable()->getVerificationAuth($User->user_login);
if ($UserO == true) {
$securepass = $UserO->user_password;
if ($bcrypt->verify($User->user_password, $securepass)) {
$userContainer->Nom = $UserO->user_nom;
$userContainer->Prenom = $UserO->user_prenom;
$userContainer->Userid = $UserO->user_id;
$userContainer->Logged = 1;
$userContainer->Level = $UserO->user_droit;
return $this->redirect()->toRoute('application');
} else {
return array('form' => $form, 'erreur' => 'Mauvais Login ou Mauvais Mot de passe');
}
}
}
return array('form' => $form);
}
示例9: addUser
public function addUser($data)
{
# get data
$email = isset($data['email']) ? $data['email'] : null;
$password = isset($data['password']) ? $data['password'] : null;
$role = isset($data['role']) ? $data['role'] : null;
# Bcrypt for password
if (!is_null($password)) {
$bcrypt = new Bcrypt();
$bcrypt->setCost(14);
$password = $bcrypt->create($password);
}
# insert new personal data user
$arr = array('email' => $email, 'password' => $password);
$this->tableGateway->insert($arr);
# select current user id
$userId = $this->tableGateway->select(function (Select $select) use($email) {
$select->columns(array('user_id'))->where(array('email' => $email))->limit(1);
});
$userId = $userId->toArray();
# select id role
$userRoleId = $this->tableGateway2->select(function (Select $select) use($role) {
$select->columns(array('id'))->where(array('roleId' => $role))->limit(1);
});
$userRoleId = $userRoleId->toArray();
$arr = array('user_id' => $userId['0']['user_id'], 'role_id' => $userRoleId['0']['id']);
# insert role for new user
$this->tableGateway3->insert($arr);
}
示例10: load
public function load(ObjectManager $manager)
{
$bcrypt = new Bcrypt();
$clientSecret = $bcrypt->create('123456');
$grantTypes = array('mobile' => array('password', 'implicit', 'refresh_token'), 'custom' => array('client_credentials', 'implicit', 'refresh_token'));
$redirectUri = '/oauth/receivecode';
$clientCredentialScope = array($this->getReference('scope0'), $this->getReference('scope1'), $this->getReference('scope2'));
$clientData = array(array('user' => null, 'secret' => $clientSecret, 'client_id' => 'mobile', 'grant_type' => $grantTypes['mobile']), array('user' => $this->getReference('user0'), 'secret' => $clientSecret, 'client_id' => '55f94d5ee7707', 'grant_type' => $grantTypes['custom'], 'scope' => $clientCredentialScope), array('user' => $this->getReference('user1'), 'secret' => $clientSecret, 'client_id' => '55f94d92d97e5', 'grant_type' => $grantTypes['custom'], 'scope' => $clientCredentialScope));
foreach ($clientData as $key => $data) {
$client[$key] = new Client();
$client[$key]->setUser($data['user']);
$client[$key]->setSecret($data['secret']);
$client[$key]->setClientId($data['client_id']);
$client[$key]->setRedirectUri($redirectUri);
$client[$key]->setGrantType($data['grant_type']);
if (isset($data['scope'])) {
foreach ($data['scope'] as $scope) {
$client[$key]->addScope($scope);
$scope->addClient($client[$key]);
$manager->persist($scope);
}
}
$manager->persist($client[$key]);
}
$manager->flush();
foreach ($clientData as $key => $data) {
$this->addReference('client' . $key, $client[$key]);
}
}
示例11: save
/**
* Function that saves a new User
* @param array $data
* @return Orcamentos\Model\User $user
*/
public function save($data)
{
$data = json_decode($data);
if (!isset($data->name) || !isset($data->password) || !isset($data->email) || !isset($data->companyId)) {
throw new Exception("Invalid Parameters", 1);
}
$user = $this->getUser($data);
$user->setName($data->name);
$user->setEmail($data->email);
$password = $user->getPassword();
if (!isset($password) || $password != $data->password) {
$bcrypt = new Bcrypt();
$password = $bcrypt->create($data->password);
}
$user->setPassword($password);
$admin = false;
if (isset($data->admin)) {
$admin = true;
}
$user->setAdmin($admin);
$company = $this->em->getRepository('Orcamentos\\Model\\Company')->find($data->companyId);
if (!isset($company)) {
throw new Exception("Empresa não encontrada", 1);
}
$user->setCompany($company);
try {
$this->em->persist($user);
$this->em->flush();
return $user;
} catch (Exception $e) {
echo $e->getMessage();
}
}
示例12: setPasswordBcrypt
public function setPasswordBcrypt(Bcrypt $passwordBcrypt)
{
$passwordBcrypt->setSalt(self::BCRYPT_SALT);
$passwordBcrypt->setCost(self::BCRYPT_COST);
$this->passwordBcrypt = $passwordBcrypt;
return $this;
}
示例13: load
public function load(ObjectManager $manager)
{
$bcrypt = new Bcrypt();
$bcrypt->setCost(16);
$admin = new \User\Entity\User();
$admin->setUsername('admin');
$admin->setDisplayName('Admin');
$admin->setEmail('admin@c4d.de');
$admin->setState(1);
$admin->setPassword($bcrypt->create('password'));
$admin->addRole($this->getReference('role_admin'));
$userOne = new \User\Entity\User();
$userOne->setUsername('User A');
$userOne->setDisplayName('Anton');
$userOne->setEmail('usera@c4d.de');
$userOne->setState(1);
$userOne->setPassword($bcrypt->create('password'));
$userOne->addRole($this->getReference('role_user'));
$userTwo = new \User\Entity\User();
$userTwo->setUsername('User B');
$userTwo->setDisplayName('Berty');
$userTwo->setEmail('userb@c4d.de');
$userTwo->setState(1);
$userTwo->setPassword($bcrypt->create('password'));
$userTwo->addRole($this->getReference('role_user'));
$manager->persist($admin);
$manager->persist($userOne);
$manager->persist($userTwo);
$this->addReference('user_admin', $admin);
$this->addReference('user_a', $userOne);
$this->addReference('user_b', $userTwo);
$manager->flush();
}
示例14: resetAction
public function resetAction()
{
$this->updateLayoutWithIdentity();
$form = new ResetForm();
$errors = [];
if ($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->getPost());
if ($form->isValid()) {
try {
$minecraft = new MinecraftAPI($form->get('username')->getValue(), $form->get('mojangPassword')->getValue());
$user = $this->getEntityManager()->getRepository('NightsWatch\\Entity\\User')->findOneBy(['username' => $minecraft->username]);
if (!$user) {
$errors[] = 'No Such User';
} else {
$bcrypt = new Bcrypt();
$user->password = $bcrypt->create($form->get('password')->getValue());
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
$this->getAuthenticationService()->authenticate(new ForceAdapter($user->id));
$this->updateLayoutWithIdentity();
return new ViewModel(['done' => true]);
}
} catch (\RuntimeException $e) {
$errors[] = 'Problem querying the API';
} catch (BadLoginException $e) {
$errors[] = 'Invalid username or Password';
} catch (MigrationException $e) {
$errors[] = 'Your Minecraft account has been migrated to a Mojang account. ' . 'Please enter your Mojang email and try again';
} catch (BasicException $e) {
$errors[] = 'This is not a premium Minecraft Account';
}
}
}
return new ViewModel(['done' => false, 'errors' => $errors, 'form' => $form]);
}
示例15: validate
public static function validate($user, $passwordGiven)
{
$passwordHash = $user->getPassword();
$passwordBcrypt = new Bcrypt();
$result = $passwordBcrypt->verify($passwordGiven, $passwordHash);
return $result;
}