Ver Mensaje Individual
  #4 (permalink)  
Antiguo 23/09/2015, 09:04
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años
Puntos: 320
Respuesta: Cual es la mejor formar de crear un Token

Código PHP:
Ver original
  1. <?php
  2.  /*/
  3.   * Crea tokens seguros de 10 caracteres.
  4.  /*/
  5.  class Token {
  6.      /*/
  7.       * Un token de 10 caracteres unico y constante
  8.       * para cada instalacion del sistema que hagas.
  9.      /*/
  10.      const SERVER_TOKEN = "827ccb0eea";
  11.  
  12.      /*/
  13.       * Genera un token de 10 caracteres.
  14.      /*/
  15.      public static function make() {
  16.  
  17.          if(function_exists("random_bytes"))
  18.              // PHP >= 7
  19.              $token = bin2hex(random_bytes(5));
  20.  
  21.          elseif(function_exists("openssl_random_pseudo_bytes"))
  22.              // OpenSSL disponible
  23.              {
  24.                  $strong = true;
  25.                  $token = bin2hex(openssl_random_pseudo_bytes(5, $strong));
  26.                  if(!$strong)
  27.                      $token = substr(md5(rand(0, 999).$token.rand(0, 999)), rand(0, 21), 10);
  28.              }
  29.  
  30.          elseif(function_exists("hash"))
  31.              // PECL Hash disponible
  32.              $token = substr(hash("sha512", uniqid("", true)), rand(0, 117), 10);
  33.  
  34.          else
  35.              // Si no hay nada mejor disponible...
  36.              $token = substr(md5(rand(0, 999).uniqid("", true).rand(0, 999)), rand(0, 21), 10);
  37.  
  38.          return substr(md5(self::SERVER_TOKEN.$token), rand(0, 21), 10);
  39.      }
  40.  }
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios