Created
June 25, 2014 20:21
-
-
Save PabloPG/0a4060cc702d3eb7f6c1 to your computer and use it in GitHub Desktop.
Bcrypt para PHP < 5.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
abstract class Bcrypt { | |
// blowfish | |
private static $algo = '$2a'; | |
// Parametro de custo | |
private static $custo = '$10'; | |
// Uso interno | |
public static function salto() { | |
return substr(sha1(mt_rand()),0,22); | |
} | |
// Criando o hash de senha | |
public static function hash($senha) { | |
return crypt($senha, | |
self::$algo . | |
self::$custo . | |
'$' . self::salto()); | |
} | |
// Comparar se a senha tem o msm hash | |
// $hash = Salvo no banco, $senha = Vindo do post | |
public static function check($hash, $senha) { | |
$salto_completo = substr($hash, 0, 29); | |
$novo_hash = crypt($senha, $salto_completo); | |
return ($hash == $novo_hash); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment