Skip to content

Instantly share code, notes, and snippets.

@TiuTalk
Created December 8, 2011 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TiuTalk/1447693 to your computer and use it in GitHub Desktop.
Save TiuTalk/1447693 to your computer and use it in GitHub Desktop.
<?php
/**
* Gera um salt aleatório
*
* @param int $tamanho Tamanho do salt
*
* @return string
*/
function geraSaltAleatorio($tamanho = 22) {
return substr(sha1(mt_rand()), 0, $tamanho);
}
$salt = geraSaltAleatorio();
// Senha do usuário, pode ter vindo do $_POST, $_GET ou outro lugar
$senha = 'olá mundo';
// Encripta a senha usando MD5
$senha = md5($senha . $salt);
// Resultado:
// c1de0ebde1fd59955ccd57ccd89ac2e9
// Salvamos $senha e $salt no banco de dados
@SimonWaters
Copy link

This is a bad way to generate a salt, and a dreadful way to store a password.

mt_rand will usually only generate only 2^31 distinct values, whilst this is a large number, it is a lot less than you might expect from 22 hex digits (2^88).

So only 2^31 times harder to crack than unsalted MD5.

Use password_hash https://secure.php.net/manual/en/function.password-hash.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment