【发布时间】:2011-03-06 14:08:46
【问题描述】:
在选择正确的算法用于会话哈希时,大小是否重要。
我最近阅读了这个article,它建议使用漩涡为会话 ID 创建一个哈希。 Whirlpool 生成一个 128 字符的哈希字符串,这个是不是太大了?
计划是将会话哈希存储在数据库中。使用 64 个字符字段 (sha256)、96 个字符字段 (sha384) 或 128 个字符字段 (whirlpool) 之间有很大区别吗?惠而浦最初提出的论点之一是速度与其他算法的对比,但从速度结果来看 sha384 并不算太差。
可以选择截断散列以使其小于 128 个字符。
我确实修改了原始代码 sn-p,以允许根据需要更改算法。
更新:有一些关于字符串被散列的讨论,所以我已经包含了代码。
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;
}
【问题讨论】:
-
不管她告诉你什么,尺寸很重要!说真的,所有这些都非常与他们今天的存储介质相比,如果你有大量的会话(数百万),更长的哈希使得冲突更少 可能,但已经非常、非常、非常不太可能。
-
我认为她只是想让我振作起来,但是是的,我不希望处理数百万次会话。我主要关心的是索引以及 DBMS 如何处理这么大的字符字段。
标签: php algorithm session hash sessionid