セッションハッシュに使用する適切なアルゴリズムを選択する場合、サイズは重要ですか。
最近この記事を読みましたが、ワールプールを使用してセッションIDのハッシュを作成することを提案しました。Whirlpoolは128文字のハッシュ文字列を生成しますが、これは大きすぎますか?
計画は、セッションハッシュをデータベースに保存することです。64文字のフィールド(sha256)、96文字のフィールド(sha384)、または128文字のフィールド(ワールプール)を使用することには、大きな違いがありますか?ワールプールについてなされた最初の議論の1つは、速度と他のアルゴリズムでしたが、速度の結果を見ると、sha384はそれほど悪くはありません。
ハッシュを切り捨てて128文字より小さくするオプションがあります。
必要に応じてアルゴリズムを変更できるように、元のコードスニペットを変更しました。
更新:文字列がハッシュされることについていくつかの議論があったので、コードを含めました。
function generateUniqueId($maxLength = null) {
$entropy = '';
// try ssl first
if (function_exists('openssl_random_pseudo_bytes')) {
$entropy = openssl_random_pseudo_bytes(64, $strong);
// skip ssl since it wasn't using the strong algo
if($strong !== true) {
$entropy = '';
}
}
// add some basic mt_rand/uniqid combo
$entropy .= uniqid(mt_rand(), true);
// try to read from the windows RNG
if (class_exists('COM')) {
try {
$com = new COM('CAPICOM.Utilities.1');
$entropy .= base64_decode($com->GetRandom(64, 0));
} catch (Exception $ex) {
}
}
// try to read from the unix RNG
if (is_readable('/dev/urandom')) {
$h = fopen('/dev/urandom', 'rb');
$entropy .= fread($h, 64);
fclose($h);
}
// create hash
$hash = hash('whirlpool', $entropy);
// truncate hash if max length imposed
if ($maxLength) {
return substr($hash, 0, $maxLength);
}
return $hash;
}