对不起,这个很奇怪的问题。我理解用于传输数据的base64编码(即MIME的Base64编码)的目的,但我不知道是否需要对base64编码我的盐。我需要base64编码我的盐(哈希密码)吗?
我写了一个实用工具类(抽象基类确实):
use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;
abstract class AbstractCryptPasswordEncoder extends BasePasswordEncoder
{
/**
* @return string
*/
protected abstract function getSaltPrefix();
/**
* @return string
*/
protected abstract function getSalt();
/**
* {@inheritdoc}
*/
public function encodePassword($raw, $salt = null)
{
return crypt($raw, $this->getSaltPrefix().$this->getSalt());
}
/**
* {@inheritdoc}
*/
public function isPasswordValid($encoded, $raw, $salt = null)
{
return $encoded === crypt($raw, $encoded);
}
}
真正的实现类将是:
class Sha512CryptPasswordEncoder extends AbstractCryptPasswordEncoder
{
/**
* @var string
*/
private $rounds;
/**
* @param null|int $rounds The number of hashing loops
*/
public function __construct($rounds = null)
{
$this->rounds = $rounds;
}
/**
* {@inheritdoc}
*/
protected function getSaltPrefix()
{
return sprintf('$6$%s', $this->rounds ? "rounds={$this->rounds}$" : '');
}
/**
* {@inheritdoc}
*/
protected function getSalt()
{
return base64_encode(openssl_random_pseudo_bytes(12));
}
}
的关键部分是盐的产生,这将嵌入密码:我是否需要base64_encode
出于任何原因(存储),假设它永远不会通过电线发送?
参考这个问题:http://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords但是,没有 - 你不应该在哈希密码上使用base64。 –