Skip to content

Instantly share code, notes, and snippets.

@PabloPG
Created June 25, 2014 20:21
Show Gist options
  • Save PabloPG/0a4060cc702d3eb7f6c1 to your computer and use it in GitHub Desktop.
Save PabloPG/0a4060cc702d3eb7f6c1 to your computer and use it in GitHub Desktop.
Bcrypt para PHP < 5.5
<?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